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";
} }

最新文章

  1. ax Mail
  2. Ubuntu16.04 802.1x 有线连接 输入账号密码,为什么连接不上?
  3. CSAPP读书随笔之一:为什么汇编器会将call指令中的引用的初始值设置为-4
  4. 浅谈Excel开发:七 Excel 自定义任务窗体
  5. [Cache] C#操作缓存--CacheHelper缓存帮助类 [复制链接]
  6. Mysql实现行列转换
  7. Oracle添加数据报文字与格式字符串不匹配错误
  8. Win2D 官方文章系列翻译 - 避免内存泄漏
  9. Beta Round #9 (酱油杯noi考后欢乐赛)随机数生成器
  10. windows server2008 r2修改远程桌面连接端口。
  11. PHP学习之-正则表达式
  12. (转载)js 快捷键大全,并有简单使用说明
  13. wpf研究之道——自定义Button控件
  14. android gradle tools 3.X中dependencies, implementation和compile区别
  15. 【WPF】ImageMagick调节图片的颜色
  16. Go Example--状态协程
  17. SSM(Spring +SpringMVC + Mybatis)框架搭建
  18. 部署python django程序
  19. September 24th 2017 Week 39th Sunday
  20. 解题:APIO 2015 雅加达的摩天大楼

热门文章

  1. 互联网金融研究组:P2P借贷平台:性质、风险与监管(上)
  2. MVC异常过滤器
  3. 学习参考《TensorFlow深度学习》高清中文版PDF+英文版PDF+源代码
  4. 紫书 例题 9-6 UVa 11400 (线性结构上的动态规划)
  5. Mysql学习总结(7)——MySql索引原理与使用大全
  6. hdoj-1421-搬寝室【DP】
  7. 49.Node.js RESTful API
  8. HTML标签大全(常用)
  9. LightOJ 1063 Ant Hills
  10. mapper提示Could not autowire. No beans of … type found?