java接收ios文件上传的示例代码

时间:2022-04-18 08:18:25

本文实例为大家分享了java如何接收ios文件上传的具体代码,供大家参考,具体内容如下

ios multipart/form-data post请求java后台spring接口一直出错,搞了两天,终于解决了,积累下来

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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 添加

?
1
2
3
4
5
6
7
8
9
10
11
<!-- 这个用于上传文件工具操作 -->
  <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

?
1
2
3
4
5
6
7
8
9
10
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件 中也不必引入上传组件包 -->
 <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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u013939884/article/details/55103919