package com.ryzw.yth.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
public class FileUtil {
//保存文件并返回保存路径
public String upload(MultipartFile file,String filepath,HttpServletRequest request) throws IOException{
//String path = request.getSession().getServletContext().getRealPath("upload");
//filepath是下载的文件位置
String fileName = file.getOriginalFilename();
File dir = new File(filepath,fileName);
if(!dir.exists()){
dir.mkdirs();
}
//MultipartFile自带的解析方法
file.transferTo(dir);
return dir.getAbsolutePath();
}
//下载路径
public String down(String filepath,HttpServletRequest request,HttpServletResponse response) throws Exception{
try {
//模拟文件,myfile.txt为需要下载的文件
//filepath是数据库中保存的文件路径
String fileName = filepath;
//获取输入流
InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
//假如以中文名下载的话
String filename = new File(fileName).getName();
//转码,免得文件名中文乱码
filename = URLEncoder.encode(filename,"UTF-8");
//设置文件下载头
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
int len = 0;
while((len = bis.read()) != -1){
out.write(len);
out.flush();
}
out.close();
return "success";
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "false";
}
}
//删除单个文件
public String doDeleteEmptyDir(String dir) {
boolean success = (new File(dir)).delete();
if (success) {
System.out.println("Successfully deleted empty directory: " + dir);
return "success";
} else {
System.out.println("Failed to delete empty directory: " + dir);
return "false";
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
public class FileUtil {
//保存文件并返回保存路径
public String upload(MultipartFile file,String filepath,HttpServletRequest request) throws IOException{
//String path = request.getSession().getServletContext().getRealPath("upload");
//filepath是下载的文件位置
String fileName = file.getOriginalFilename();
File dir = new File(filepath,fileName);
if(!dir.exists()){
dir.mkdirs();
}
//MultipartFile自带的解析方法
file.transferTo(dir);
return dir.getAbsolutePath();
}
//下载路径
public String down(String filepath,HttpServletRequest request,HttpServletResponse response) throws Exception{
try {
//模拟文件,myfile.txt为需要下载的文件
//filepath是数据库中保存的文件路径
String fileName = filepath;
//获取输入流
InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
//假如以中文名下载的话
String filename = new File(fileName).getName();
//转码,免得文件名中文乱码
filename = URLEncoder.encode(filename,"UTF-8");
//设置文件下载头
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
int len = 0;
while((len = bis.read()) != -1){
out.write(len);
out.flush();
}
out.close();
return "success";
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "false";
}
}
//删除单个文件
public String doDeleteEmptyDir(String dir) {
boolean success = (new File(dir)).delete();
if (success) {
System.out.println("Successfully deleted empty directory: " + dir);
return "success";
} else {
System.out.println("Failed to delete empty directory: " + dir);
return "false";
}
}