Front-end test page code:<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('Uploaded files cannot exceed 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> Backend servlet receiving codepackage 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")+"/"; //Define the path String tmpPath=JTKit.getBaseDIR()+uri; //This is the personal project path, define the path according to your needs DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setRepository(new File(tmpPath));//temporary file storage path ServletFileUpload fileUpload = new ServletFileUpload(factory);//core operation object fileUpload.setHeaderEncoding("utf-8");//anti-garbled code try { //If you want to force the conversion in real time, you need to download the jar package (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){//Indicates that a maximum of 1024 bytes can be read each time 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(); } } } Test Results Supplement: Servlet obtains data submitted by the form In the Servlet's doPost method:protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } To get the form data, first of all, in order to prevent the problem of Chinese garbled characters, you need to set the encoding of the request to "UTF-8": request.setCharacterEncoding("utf-8"); How to get a single string:String username = request.getParameter("username"); How to get string array:String[] favorites = request.getParameterValues("favorite"); The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me. You may also be interested in:
|
<<: Example of how to automatically start an application service in a Docker container
>>: How to modify the initial password of MySQL on MAC
Note When developing an article display list inte...
Table of contents Preface Introduction Live Easy ...
Preface I recently installed MySQL 5.7 and found ...
It is troublesome to install the db2 database dir...
Preface: Sometimes in a route, the main part is t...
Using UNION Most SQL queries consist of a single ...
1. Download the accelerated version of msyql dock...
Ideas: An outer box sets the background; an inner...
1. Pull the MySQL image Get the latest MySQL imag...
load Request Success Request failed Click cmd and...
This article shares the specific code for the WeC...
Table of contents 1. Introduction 2. Back up the ...
Public free STUN servers When the SIP terminal us...
<br />A contradiction arises. In small works...
Preface Learn to create a file system on your sys...