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. [Chromium文档转载,第005章]Calling Mojo from Blink

    For Developers‎ > ‎Design Documents‎ > ‎Mojo‎ > ‎ Calling Mojo from Blink Variants Let's as ...

  2. 使用js实现简单放大镜的效果

    实现原理:使用2个div,里面分别放大图片和小图片,在小图片上应该还有一个遮罩层,通过定位遮罩层的位置来定位大图片的相对位置,而且,遮罩层的移动应该和大图片的移动方向相反 关键: 大图片和小图片大小比 ...

  3. 【Redis发布订阅】

    Redis通过PUBLISH.SUBSCRIBE等命令实现发布与订阅模式. 举例:QQ群的公告,单个发布者,多个收听着. *** 发布/订阅 PUBLISH 频道 消息 将消息发布到指定的频道. . ...

  4. 彻底分析虚拟视频驱动vivi(三)

    在Ubuntu系统中接上usb摄像头设备时,系统会自动安装对应的usb设备驱动程序.我们现在要使用自己编译的vivi驱动,该怎么办呢? 1.先安装系统自带的vivi驱动和它所依赖的所有驱动:sudo ...

  5. Centos7最小化安装后再安装图形界面

    1. yum -y groupinstall "X Window System" 2. yum -y groupinstall "GNOME Desktop"  ...

  6. C++ 不输入回车就不结束输入的实现方法

    方法一 char mystr[128]; mystr[0] = '\0'; char c = getchar(); int i=0; while(c!='\n') { mystr[i] = c; c ...

  7. The incident LOST_EVENTS occured on the master. Message: error writing to the binary log, Error_code

    1 mysq error日志报错例如以下: 2014-05-12 11:29:54 22977 [ERROR] Slave SQL: The incident LOST_EVENTS occured ...

  8. 【Android实战】Bitmap图片的截屏、模糊处理、传递、使用

    项目中遇到了这样一个需求: 当某个条件满足时就截取当前屏幕.并跳转到另外一个页面,同一时候将这个截屏图片作为下一个页面的背景图片,同一时候背景图片须要模糊处理 接下来就一步一步解决这个问题: 1.截取 ...

  9. Intersection between a 2d line and a conic in OpenCASCADE

    Intersection between a 2d line and a conic in OpenCASCADE eryar@163.com Abstract. OpenCASCADE provid ...

  10. Bmob移动后端云服务平台--Android从零開始--(一)何为Bmob

    Bmob移动后端云服务平台--Android从零開始--(一)何为Bmob 在正式的项目开发中,单client不能满足我们的需求,须要实现client与服务端的连接. 而在编写Android服务端代码 ...