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

网友投稿 347 2023-02-01


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

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

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

package com.xx.controller;

import java.io.FilDvPvIKkeOutputStream;

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 files = new LinkedList();

FileMeta fileMeta = null;

/**************************DvPvIKk*************************

* URL: /rest/controller/upload upload(): receives files

*

* @param request

* : MultipartHttpServletRequest auto passed

* @param response

* : HttpServletResponse auto passed

* @return LinkedList 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 iterDvPvIKk = 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";

DvPvIKk}

}

pom.xml 添加

commons-fileupload

commons-fileupload

1.3.1

commons-io

commons-io

2.4

spring-config.xml 添加bean

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:关于什么是入侵防御系统ips的信息
下一篇:在java中线程状态(java中线程状态有哪些)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~