文件上传流式处理commons-fileupload
1. 从请求中获取MultipartFile
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(
@RequestParam("file") MultipartFile file
) throws IOException{
2. 文件流处理
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile; public class UploadFileUtil {
/**
* 使用相同的名称
* @param file
* @param filePath
* @return
*/
public static String handleFileUpload(MultipartFile file,String filePath){
try {
if(!file.isEmpty()){
String name=file.getOriginalFilename();
InputStream inpustream=file.getInputStream();
FileOutputStream fos = new FileOutputStream(filePath+File.separator+name);
Streams.copy(inpustream, fos, true);
}else{
return "can not upload this file,because it is empty!";
}
} catch (IOException e) {
return "failed to upload this file,because Stream interrupt unexpectedly";
}
return "upload file sucess";
} /**
* 重命名文件,包含后缀
* @param file
* @param filePath
* @param fileName
* @return
*/
public static String handleFileUpload(MultipartFile file,String filePath,String fileName){
try {
if(!file.isEmpty()){
InputStream inpustream=file.getInputStream();
FileOutputStream fos = new FileOutputStream(filePath+File.separator+fileName);
Streams.copy(inpustream, fos, true);
}else{
return "can not upload this file,because it is empty!";
}
} catch (IOException e) {
return "failed to upload this file,because Stream interrupt unexpectedly";
}
return "upload file sucess";
} /**
*Copy bytes from a large (over 2GB) file with same filename
* @param file
* @param filePath
* @param fileName
* @return
*/ public static String handleLargeFileUpload(MultipartFile file,String filePath){
try {
if(!file.isEmpty()){
String name=file.getOriginalFilename();
InputStream inpustream=file.getInputStream();
FileOutputStream fos = new FileOutputStream(filePath+File.separator+name);
IOUtils.copyLarge(inpustream, fos);
if(inpustream!=null){
inpustream.close();
}
if(fos!=null){
fos.close();
}
}else{
return "can not upload this file,because it is empty!";
}
} catch (IOException e) {
return "failed to upload this file,because Stream interrupt unexpectedly";
}
return "upload file sucess";
} /**
*Copy bytes from a large (over 2GB) file with rename file
* @param file
* @param filePath
* @param fileName
* @return
*/ public static String handleLargeFileUpload(MultipartFile file,String filePath,String fileName){
try {
if(!file.isEmpty()){
InputStream inpustream=file.getInputStream();
FileOutputStream fos = new FileOutputStream(filePath+File.separator+fileName);
IOUtils.copyLarge(inpustream, fos);
if(inpustream!=null){
inpustream.close();
}
if(fos!=null){
fos.close();
}
}else{
return "can not upload this file,because it is empty!";
}
} catch (IOException e) {
return "failed to upload this file,because Stream interrupt unexpectedly";
}
return "upload file sucess";
} }
文件上传流式处理commons-fileupload的更多相关文章
- Ajax文件上传三式
文件上传(三式) 1.urls.py文件 url(r'^upload.html$', views.upload), 2.views.py文件 import os def upload(request) ...
- springboot文件上传 流的方式 后台计算上传进度
//代码 public static void main(String[] args) throws Exception { String path = "f:/svn/t_dictiona ...
- 文件上传控件asp:FileUpload
前端 使用的控件<asp:FileUpload ID="fileup" runat="server" /><span class=" ...
- Spring MVC的文件上传和下载
简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...
- Spring MVC使用commons fileupload实现文件上传功能
通过Maven建立Spring MVC项目,引入了Spring相关jar依赖. 1.为了使用commons fileupload组件,需要在pom.xml中添加依赖: <properties&g ...
- SpringMVC学习总结(六)——SpringMVC文件上传例子(2)
基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下使用SpringMVC进行表单上的文件上传以及多个文件同时上传的不同方法 一.配置文件: SpringMVC 用的是 的 ...
- Strut2 和Spring MVC 文件上传对比
在Java领域中,有两个常用的文件上传项目:一个是Apache组织Jakarta的Common-FileUpload组件 (http://commons.apache.org/proper/commo ...
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile(转)
文件上传项目的源码下载地址:http://download.csdn.net/detail/swingpyzf/6979915 一.配置文件:SpringMVC 用的是 的MultipartFil ...
- SpringMVC 使用MultipartFile实现文件上传(转)
http://blog.csdn.net/kouwoo/article/details/40507565 一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们 ...
随机推荐
- 如何快速复制Windows警告提示消息对话框内容
凡是使用过计算机的朋友,都遇到过系统发出的警告提示消息对话框,如图所示. 哇!好长的一串英文错误警告,这要手写到什么时候呢?不!现在不用这么麻烦了. 你只要鼠标选中这个提示框Ctrl+C,然后打开你的 ...
- Codeforces 701A. Cards(水)
A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- 添加 validate 验证规则
上篇文章链接:http://blog.csdn.net/chenmoimg_/article/details/71191476 修改 msg.js $.extend($.validator.messa ...
- 使用python制作二维码
python-qrcode是个用来生成二维码图片的第三方模块,主要依赖的是 PIL 模块和 qrcode 库.(PIL模块只支持python2.7及以下版本,python3之后无法使用,官方推荐pyt ...
- JavaScript笔记(6)
一.Date Date实例用来处理日期和时间.Date对象基于1970年1月1日(格林威治时间)世界标准时间起经过的毫秒数.常用:new Date();new Date(value);new Date ...
- 紫书 习题 10-25 UVa 1575 (有重复元素的全排列+暴搜)
我一开始以为有什么很牛逼的方法来做,然后一直没有思路 后来看了https://blog.csdn.net/zju2016/article/details/78562932的博客 竟然是暴搜?????? ...
- 机房收费系统——UML类图
在对一个软件系统进行设计和建模的时候,一般是从构造系统的基本词汇開始,包含构造这些词汇的基本属性和行为. 系统分析师假设要对所设计的系统清晰认识.还有考虑这些基本词汇之间的关系.而假设把这些行为可视化 ...
- FormData是什么
FormData是什么 一.总结 一句话总结:利用 FormData 对象,可以通过JavaScript键值对来模拟一系列表单控件,还可以使用 XMLHttpRequest的send() 方法来异步提 ...
- es6 --- var const let
var const let 区别 今天第一次遇到const定义的变量,查阅了相关资料整理了这篇文章.主要内容是:js中三种定义变量的方式const, var, let的区别. 1. const ...
- Spark RPC
在Spark中,对于网络调用的底层封装(粘包拆包,编解码,链路管理等)都是在common/network-common包中实现的(详见[common/network-common]).在common/ ...