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();
}
}
原创粉丝点击