This article shares the specific code of how Java receives ios file upload for your reference. The specific content is as follows
ios Multipart/form-data POST request java background spring interface keeps error. After two days of work, it finally solved it and accumulated
package com.xx.controller;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import java.util.LinkedList;import java.util.List;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 org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import com.nupaApp.model.FileMeta;@Controller@RequestMapping("/controller")public class File1Controller { LinkedList<FileMeta> files = new LinkedList<FileMeta>(); FileMeta fileMeta = null; /****************************************************************** * URL: /rest/controller/upload upload(): receives files * * @param request * : MultipartHttpServletRequest auto passed * @param response * : HttpServletResponse auto passed * @return LinkedList<FileMeta> as json format * @throws IOException * @throws FileUploadException ******************************************************/ @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public String upload(HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException { boolean isMultipart = ServletFileUpload.isMultipartContent(request);// Determine whether it is the form file type DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload sfu = new ServletFileUpload(factory); List items = sfu.parseRequest(request);// Get a list of all uploaded fields from request for (Iterator iter = items.iterator(); iter.hasNext();) { FileItem fileitem = (FileItem) iter.next(); if (!fileitem.isFormField() && fileitem != null) {// The interpretation is not an ordinary form field or a file // Operate the file item file steps to get the size and path // Define the image output path String imgPath = "e:" + System.currentTimeMillis() + ".jpg"; // Define the image stream InputStream fin = fileitem.getInputStream(); // Define the image output stream FileOutputStream fout = new FileOutputStream(imgPath); // Write the file byte[] b = new byte[1024]; int length = 0; while ((length = fin.read(b)) > 0) { fout.write(b, 0, length); } // Close the data stream fin.close(); fout.close(); } } return "200"; }}pom.xml Add
<!-- This is used for file upload tool operation--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
spring-config.xml Add bean
<!-- Configuration file upload. If you do not use file upload, you do not need to configure it. Of course, if you do not match, then there is no need to introduce the upload component package in the configuration file--> <bean id="multipartResolver " > <!-- Default encoding--> <property name="defaultEncoding" value="utf-8" /> <!-- Maximum file size--> <property name="maxUploadSize" value="10485760000" /> <!-- Maximum value in memory--> <property name="maxInMemorySize" value="40960" /> </bean>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.