springmvc框架的文件上传下载工具类
来源:互联网 发布:旗帜软件照片处理器 编辑:程序博客网 时间:2024/06/05 12:41
package com.track.util;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.Date;
/**
* 调用实例:
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
MultipartFile multipartFile = fileMap.get("file1");
String originalFilename = multipartFile.getOriginalFilename(); //004.jpg 为空就说明没有上传文件
String url=null;
if (originalFilename!=null) {
url = FileUtils.upload(multipartFile, photoPath);
}
*/
/**
* SSM框架的的文件上传,下载工具类
*/
private static final String path = "/Upload/";
//上传图片路径
public static final String photoPath = path+"Photo/";
//默认图片
public static final String defaultImg="/Public/images/Photo_03.png";
public static final String defaultSignature="/Public/images/images/qianm_03.png";
//获取服务器路径
public String systemPath(){
URL u = this.getClass().getResource("/");
System.out.println(u);
String c = this.getClass().getResource("/").getPath();
System.out.println(c);
try {
c = java.net.URLDecoder.decode(c, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(c);
String pp = c.substring(1, c.lastIndexOf("WEB-INF"));
System.out.println(pp);
return pp;
}
public class FileUtils {
// 上传路径
private static String systemPath = systemPath();
/**
* function:文件上传类
*/
public static String upload(MultipartFile multipartFile, String path) throws IOException {
String originalFilename = multipartFile.getOriginalFilename();
if (originalFilename.trim() != "") {
String fileType = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
String fileName = DateUtils.format(new Date(), DateUtils.DATE_TIME_ALL) + fileType;
File dir = new File(systemPath+path, fileName);
if (!dir.exists()) {
dir.mkdirs();
}
// MultipartFile自带的解析方法
multipartFile.transferTo(dir);
//return dir.toString();
return fileName;
}
return "";
}
/**
* function:文件下载类
*/
public static void downloadTemplate(HttpServletResponse response, String path) throws IOException {
File file = new File(path);// path是根据日志路径和文件名拼接出来的
String filename = file.getName();// 获取文件名称
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);// 输出文件
os.flush();
os.close();
}
}
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.Date;
/**
* 调用实例:
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
MultipartFile multipartFile = fileMap.get("file1");
String originalFilename = multipartFile.getOriginalFilename(); //004.jpg 为空就说明没有上传文件
String url=null;
if (originalFilename!=null) {
url = FileUtils.upload(multipartFile, photoPath);
}
*/
/**
* SSM框架的的文件上传,下载工具类
*/
private static final String path = "/Upload/";
//上传图片路径
public static final String photoPath = path+"Photo/";
//默认图片
public static final String defaultImg="/Public/images/Photo_03.png";
public static final String defaultSignature="/Public/images/images/qianm_03.png";
//获取服务器路径
public String systemPath(){
URL u = this.getClass().getResource("/");
System.out.println(u);
String c = this.getClass().getResource("/").getPath();
System.out.println(c);
try {
c = java.net.URLDecoder.decode(c, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(c);
String pp = c.substring(1, c.lastIndexOf("WEB-INF"));
System.out.println(pp);
return pp;
}
public class FileUtils {
// 上传路径
private static String systemPath = systemPath();
/**
* function:文件上传类
*/
public static String upload(MultipartFile multipartFile, String path) throws IOException {
String originalFilename = multipartFile.getOriginalFilename();
if (originalFilename.trim() != "") {
String fileType = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
String fileName = DateUtils.format(new Date(), DateUtils.DATE_TIME_ALL) + fileType;
File dir = new File(systemPath+path, fileName);
if (!dir.exists()) {
dir.mkdirs();
}
// MultipartFile自带的解析方法
multipartFile.transferTo(dir);
//return dir.toString();
return fileName;
}
return "";
}
/**
* function:文件下载类
*/
public static void downloadTemplate(HttpServletResponse response, String path) throws IOException {
File file = new File(path);// path是根据日志路径和文件名拼接出来的
String filename = file.getName();// 获取文件名称
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);// 输出文件
os.flush();
os.close();
}
}
阅读全文
0 0
- springmvc框架的文件上传下载工具类
- 【框架学习】SpringMVC上传下载文件
- SpringMVC的文件上传下载
- SSM框架-SpringMVC 实例文件上传下载
- SSM框架-SpringMVC 实例文件上传下载
- SSM框架-SpringMVC 实例文件上传下载
- SSM框架-SpringMVC 实例文件上传下载
- SSM框架-SpringMVC 实例文件上传下载
- 文件上传下载工具类
- springMVC实现文件的上传下载
- 文件上传下载删除工具类
- SpringMVC下Excel文件的上传下载实现
- SpringMVC笔记(五)文件的上传下载
- 基于SpringMVC的文件上传下载功能
- springmvc的乱码问题及文件上传下载
- SpringMVC比较全面的文件上传下载
- springmvc文件上传下载简单实现案例(ssm框架使用)
- springmvc文件上传下载简单实现案例(ssm框架使用)
- 数组去重的三种方法
- 运行oozie自带mapreduce例子
- 最新版 FFmpeg ios 添加依赖库编译
- 电气事故如何避免
- 解决get和post请求 全部乱码
- springmvc框架的文件上传下载工具类
- Volatile变量和同步机制synchronized 、Lock区别
- android-关于Gson和json的解析转化使用
- 8个超实用的jQuery技巧攻略
- 【ulua学习笔记】SimpleFramework使用步骤总结
- Nodejs Error: Cannot find module 当前文件夹下的模块
- cocos2dx 浅析cocos2dx3.X引擎目录
- 【PAT】【Advanced Level】1113. Integer Set Partition (25)
- IntelliJ IDEA搭建Springboot项目解决热加载问题