vue form表单post请求结合Servlet实现文件上传功能
程序员文章站
2022-03-26 18:41:30
前端测试页面代码:
前端测试页面代码:
<template> <div> <input type="file" name="file" @change="change($event)"> </div> </template> <script> export default { created(){ this.path = this.$route.query; for (let i in this.path) { this[i] = decodeuricomponent(this.path[i]); } }, methods:{ change(ev){ let file = ev.target.files[0]; let size = file.size; let name = file.name; if(size > 314572800){ this.$message.warning('上传文件不能超过300m'); return; } let formdata = new formdata(); formdata.append('file',file,name) this.$axios.post('/jt3'+this.getddrecorddelete,formdata,{ headers:{"content-type":"multipart/form-data"} }).then(data=>{ console.log(data); }) } } } </script> <style scoped> </style>
后端servlet接收代码
package jt3.control.zygkh; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.util.list; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.apache.commons.fileupload.fileitem; import org.apache.commons.fileupload.fileuploadexception; import org.apache.commons.fileupload.disk.diskfileitemfactory; import org.apache.commons.fileupload.servlet.servletfileupload; import jtacc.filter.jtkit; import jtacc.jtpub.dt; @webservlet(urlpatterns = "/upfile/file") public class uploadservlet extends httpservlet { private static final long serialversionuid = 1l; protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { system.out.println(11); this.dopost(request, response); } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string uri="/u/file/"+dt.getformatdate("yyyymmdd")+"/"; //定义路径 string tmppath=jtkit.getbasedir()+uri;//此处为个人项目路径,根据需求定义路径 diskfileitemfactory factory = new diskfileitemfactory(); factory.setrepository(new file(tmppath));//临时文件存储路径 servletfileupload fileupload = new servletfileupload(factory);//核心操作对象 fileupload.setheaderencoding("utf-8");//防乱码 try { //此处如果要实时强行转换则需要下载jar包(commons-fileupload-1.3.3.jar) list<fileitem> list = fileupload.parserequest(request); for (fileitem fileitem : list) { inputstream in = fileitem.getinputstream(); string filename = fileitem.getname(); if (fileitem != null) { system.out.println(filename); int len = 0; byte[] array = new byte[1024]; fileoutputstream fos = new fileoutputstream(tmppath+filename); while((len = in.read(array))!=-1){//表示每次最多读1024个字节 fos.write(array,0,len); fos.flush(); } fos.close(); in.close(); fileitem.delete(); response.setcharacterencoding("utf-8"); string realpath = uri+filename; response.getwriter().append(realpath); } } } catch (fileuploadexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
测试结果
补充:servlet获取表单提交过来的数据
在servlet的dopost方法:
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { }
中获取表单数据,首先,为了防止出现中文乱码问题,需要给request设置编码为“utf-8”:
request.setcharacterencoding("utf-8");
获取单个字符串的方式:
<pre style="font-family: 宋体; font-size: 12pt; background-color: rgb(255, 255, 255);"><span style="font-size:18px; font-family: arial, helvetica, sans-serif; background-color: rgb(240, 240, 240);">string username = request.getparameter("username");</span>
获取字符串数组的方式:
string[] favorites = request.getparametervalues("favorite");
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。