java接收ios文件上传的示例代码
程序员文章站
2023-12-15 16:48:58
本文实例为大家分享了java如何接收ios文件上传的具体代码,供大家参考,具体内容如下
ios multipart/form-data post请求java后台sprin...
本文实例为大家分享了java如何接收ios文件上传的具体代码,供大家参考,具体内容如下
ios multipart/form-data post请求java后台spring接口一直出错,搞了两天,终于解决了,积累下来
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);// 判断是否是表单文件类型 diskfileitemfactory factory = new diskfileitemfactory(); servletfileupload sfu = new servletfileupload(factory); list items = sfu.parserequest(request);// 从request得到所有上传域的列表 for (iterator iter = items.iterator(); iter.hasnext();) { fileitem fileitem = (fileitem) iter.next(); if (!fileitem.isformfield() && fileitem != null) {// 判读不是普通表单域即是file // 操作fileitem文件步骤,可以获取大小、路径 // 定义图片输出路径 string imgpath = "e:" + system.currenttimemillis() + ".jpg"; // 定义图片流 inputstream fin = fileitem.getinputstream(); // 定义图片输出流 fileoutputstream fout = new fileoutputstream(imgpath); // 写文件 byte[] b = new byte[1024]; int length = 0; while ((length = fin.read(b)) > 0) { fout.write(b, 0, length); } // 关闭数据流 fin.close(); fout.close(); } } return "200"; } }
pom.xml 添加
<!-- 这个用于上传文件工具操作 --> <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 添加bean
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件 中也不必引入上传组件包 --> <bean id="multipartresolver " class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <!-- 默认编码 --> <property name="defaultencoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxuploadsize" value="10485760000" /> <!-- 内存中的最大值 --> <property name="maxinmemorysize" value="40960" /> </bean>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。