java接收ios文件上傳的示例代碼
本文實例為大家分享了java如何接收ios文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
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);
}
// 關(guān)閉數(shù)據(jù)流
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
<!-- 配置文件上傳,如果沒有使用文件上傳可以不用配置,當(dāng)然如果不配,那么配置文件 中也不必引入上傳組件包 --> <bean id="multipartResolver " class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默認(rèn)編碼 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 內(nèi)存中的最大值 --> <property name="maxInMemorySize" value="40960" /> </bean>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS實現(xiàn)文件上傳功能
- vue使用axios實現(xiàn)文件上傳進(jìn)度的實時更新詳解
- ios用AFN進(jìn)行文件上傳的示例代碼
- iOS利用AFNetworking實現(xiàn)文件上傳的示例代碼
- iOS 斷點(diǎn)上傳文件的實現(xiàn)方法
- iOS大文件的分片上傳和斷點(diǎn)上傳的實現(xiàn)代碼
- iOS實現(xiàn)文件切片儲存并且上傳(仿斷點(diǎn)續(xù)傳機(jī)制)
- vue項目中使用axios上傳圖片等文件操作
- iOS開發(fā)中以application/json上傳文件實例詳解
- Vue axios 中提交表單數(shù)據(jù)(含上傳文件)
- iOS開發(fā)中文件的上傳和下載功能的基本實現(xiàn)
- IOS開發(fā)教程之put上傳文件的服務(wù)器的配置及實例分享
相關(guān)文章
OpenFeign無法遠(yuǎn)程調(diào)用問題及解決
文章介紹了在使用Feign客戶端時遇到的讀超時問題,并分析了原因是系統(tǒng)啟動時未先加載Nacos配置,為了解決這個問題,建議將Nacos配置放在`bootstrap.yml`文件中,以便項目啟動時優(yōu)先加載Nacos配置2024-11-11
springboot中非容器類如何獲取配置文件數(shù)據(jù)
這篇文章主要介紹了springboot中非容器類如何獲取配置文件數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
MyBatis中的@SelectProvider注解源碼分析
這篇文章主要介紹了MyBatis中的@SelectProvider注解源碼分析,@SelectProvider功能就是用來單獨(dú)寫一個class類與方法,用來提供一些xml或者注解中不好寫的sql,今天就來說下這個注解的具體用法與源碼,需要的朋友可以參考下2024-01-01
Intellij IDEA創(chuàng)建spring-boot項目的圖文教程
本文通過圖文并茂的形式給大家介紹了Intellij IDEA創(chuàng)建spring-boot項目的教程,本文給大家介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友參考下吧2018-01-01
構(gòu)建springboot自動生成mapper文件和dao接口項目的步驟和配置方法
這篇文章主要介紹了構(gòu)建springboot自動生成mapper文件和dao接口項目的步驟和配置方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
FluentMybatis實現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法
本文主要介紹了FluentMybatis實現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08

