vue form表單post請(qǐng)求結(jié)合Servlet實(shí)現(xiàn)文件上傳功能
<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(’上傳文件不能超過(guò)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;//此處為個(gè)人項(xiàng)目路徑,根據(jù)需求定義路徑 DiskFileItemFactory factory = new DiskFileItemFactory();factory.setRepository(new File(tmpPath));//臨時(shí)文件存儲(chǔ)路徑ServletFileUpload fileUpload = new ServletFileUpload(factory);//核心操作對(duì)象fileUpload.setHeaderEncoding('utf-8');//防亂碼try {//此處如果要實(shí)時(shí)強(qiáng)行轉(zhuǎn)換則需要下載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個(gè)字節(jié)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 blocke.printStackTrace();} } }
測(cè)試結(jié)果

補(bǔ)充:Servlet獲取表單提交過(guò)來(lái)的數(shù)據(jù)
在Servlet的doPost方法:protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
中獲取表單數(shù)據(jù),首先,為了防止出現(xiàn)中文亂碼問(wèn)題,需要給request設(shè)置編碼為“UTF-8”:
request.setCharacterEncoding('utf-8');獲取單個(gè)字符串的方式:
<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>獲取字符串?dāng)?shù)組的方式:
String[] favorites = request.getParameterValues('favorite');
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. django queryset相加和篩選教程2. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算3. Java實(shí)現(xiàn)的迷宮游戲4. idea設(shè)置提示不區(qū)分大小寫的方法5. JS圖片懶加載庫(kù)VueLazyLoad詳解6. Java利用TCP協(xié)議實(shí)現(xiàn)客戶端與服務(wù)器通信(附通信源碼)7. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟8. Java PreparedStatement用法詳解9. Spring如何集成ibatis項(xiàng)目并實(shí)現(xiàn)dao層基類封裝10. IDEA 2020.1.2 安裝教程附破解教程詳解

網(wǎng)公網(wǎng)安備