文件上传流式处理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来进行文件上传 所以我们 ...
随机推荐
- 机器学习(四) 分类算法--K近邻算法 KNN (上)
一.K近邻算法基础 KNN------- K近邻算法--------K-Nearest Neighbors 思想极度简单 应用数学知识少 (近乎为零) 效果好(缺点?) 可以解释机器学习算法使用过程中 ...
- Important Abstractions and Data Structures
For Developers > Coding Style > Important Abstractions and Data Structures 目录 1 TaskRunne ...
- 利用Python网络爬虫抓取微信好友的签名及其可视化展示
前几天给大家分享了如何利用Python词云和wordart可视化工具对朋友圈数据进行可视化,利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例,以及利用Python网络爬虫抓取微信好友的所 ...
- Python——Pygame实现生命游戏(game of life)
模块:pygame import pygame,sys,time,random from pygame.locals import * """Color"&qu ...
- CSUOJ 1525 Algebraic Teamwork
Problem A Algebraic Teamwork The great pioneers of group theory and linear algebra want to cooperate ...
- ecshop微信通中微信自动登录的设置方法
ecshop微信通中微信自动登录的设置方法 来 源:共享世纪 作 者:网络 时间:2015-12-03 点击: 4017 注意:微信自动登录,必须同时满足两个条件: 第一.微信公众号必须是服务号经过认 ...
- HDU——T 1075 What Are You Talking About
http://acm.hdu.edu.cn/showproblem.php?pid=1075 Time Limit: 10000/5000 MS (Java/Others) Memory Lim ...
- jquery事件 【mousedown与mouseup ----keydown与keypress与keyup】focus--blur--orrer--pageX-pageY
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> ...
- Chromium Graphics: Android L平台上WebView的变化及其对浏览器厂商的影响分析
原创文章.转载请以链接形式注明原始出处为http://blog.csdn.net/hongbomin/article/details/40799167. 摘要:Google近期公布的Android L ...
- js---14公有私有成员方法
var ns1 = {}; //命名空间 ns1.ns11 = {};//子命名空间 ns1.module1 = {name:"a",m:function(){}}; consol ...