亲宝软件园·资讯

展开

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

小小Blog 人气:0

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>

加载全部内容

相关教程
猜你喜欢
用户评论