springmvc框架的文件上传下载工具类

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();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值