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的更多相关文章

  1. Ajax文件上传三式

    文件上传(三式) 1.urls.py文件 url(r'^upload.html$', views.upload), 2.views.py文件 import os def upload(request) ...

  2. springboot文件上传 流的方式 后台计算上传进度

    //代码 public static void main(String[] args) throws Exception { String path = "f:/svn/t_dictiona ...

  3. 文件上传控件asp:FileUpload

    前端 使用的控件<asp:FileUpload ID="fileup" runat="server" /><span class=" ...

  4. Spring MVC的文件上传和下载

    简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...

  5. Spring MVC使用commons fileupload实现文件上传功能

    通过Maven建立Spring MVC项目,引入了Spring相关jar依赖. 1.为了使用commons fileupload组件,需要在pom.xml中添加依赖: <properties&g ...

  6. SpringMVC学习总结(六)——SpringMVC文件上传例子(2)

    基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下使用SpringMVC进行表单上的文件上传以及多个文件同时上传的不同方法 一.配置文件: SpringMVC 用的是 的 ...

  7. Strut2 和Spring MVC 文件上传对比

    在Java领域中,有两个常用的文件上传项目:一个是Apache组织Jakarta的Common-FileUpload组件 (http://commons.apache.org/proper/commo ...

  8. SpringMVC 文件上传配置,多文件上传,使用的MultipartFile(转)

    文件上传项目的源码下载地址:http://download.csdn.net/detail/swingpyzf/6979915   一.配置文件:SpringMVC 用的是 的MultipartFil ...

  9. SpringMVC 使用MultipartFile实现文件上传(转)

    http://blog.csdn.net/kouwoo/article/details/40507565 一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们 ...

随机推荐

  1. netflix zuul 1.x与zuul2.x之比较

    1.zuul 1.x的架构如下所示: 线程模型: 其web应用的web.xml <?xml version="1.0" encoding="UTF-8"? ...

  2. 用Struts2搭建一个登录例子【本人亲测好用】

    今天尝试struts2的搭建,遇到不少的问题,终于一一解决,逛了很多地方,最终完成搭建 1.首先要下载struts2的一些组件,我下载的是版本2.3.4.1,Eclipse是4.6版本的.由于版本的不 ...

  3. 开源计算机集群监控Ganglia应用视频

    Ganglia源于Berkeley发起的一个开源集群监视项目,设计用于监测数上千节点的计算机集群.它包含gmond.gmetad以及一个Web前端.可以用来监控系统处理器 .内存.硬盘 I/O.网络流 ...

  4. Debian9.5解决在root用户下启用VNC后lightdm+Xfce4只能使用root登录,其他用户无法登陆

    Debian9.5解决在root用户下启用VNC后lightdm+Xfce4只能使用root登录,其他用户无法登陆 最近在研究debian9.5 VNC的问题,当VNC使用tightVNC或tiger ...

  5. 如何解决winxp访问win10共享打印机提示凭据不足

    Winxp访问win10共享的打印机,提示凭据不足时,如何解决,本篇经验将和大家分享下解决的办法,希望对大家的工作有所帮助! 方法/步骤     在win10的电脑上对着始按钮点鼠标右键,点击运行,或 ...

  6. 织梦DedeCMS判断简略标题为空时则显示完整标题

    使用织梦DedeCMS系统程序开发网站中,我们会遇到很多因网页版面设计限定的宽度,使文章标题需要进行字数限制,通常做法是在a标签中加入一个title属性,让鼠标放上去的时候显示完整标题.但是标题被剪裁 ...

  7. Floyd-Warshall 算法-- 最短路径(适合节点密集的图)

     由于此算法时间复杂度为O(V³).大多数情况下不如迪杰斯特拉算法的.迪杰斯特拉算法适合于节点疏散的图.  演示样例图例如以下:     Step 1 创建节点与边的最短路径结果表(直接可达关系).数 ...

  8. 云计算时代告别phpMyAdmin

    云计算时代告别phpMyAdmin phpMyAdmin是一款很经典的MySQL数据库管理工具,在云计算快速发展的今天,phpMyAdmin交互老旧.已经不能适应时代步伐.因此有很多人開始选择一些更高 ...

  9. 具有可视化的功能的一款开源软件Gource

    今天为大家介绍一个非常有趣儿的开源软件,Gource可以将代码版本控制系统里面的日志全部可视化,也就是说可以看见每个成员在系统里面提交代码的行为,Gource目前支持git,hg,svn. 650) ...

  10. Java(标识符,关键字,注释,常量,变量)

    标识符 在java程序中有些名字是可以自定义的,那么这些自定义的名字我们就称作为自定义的标识符. 标识符要注意的细节: 标识符的组成元素是由字母(a-zA-Z).数字(0-9).下划线(_).美元符号 ...