目前的util类 导入即可使用,一些jar需要导入找一些导入即可
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtil {
/**
* 单个文件上传
*
* @return -1、上传失败 1、上传成功
*/
public static int uploadSingle(MultipartFile file, String fileName, String path){
try {
// fileName = file.getOriginalFilename();
System.out.println("------------单个文件上传路径"+path+"/"+fileName);
File newFile = new File(path+"/"+fileName);
File newFile1 = new File(path);
if (!newFile1.exists()&& !newFile1.isDirectory()) {
newFile1.mkdir();
}
InputStream inputStream = file.getInputStream();
FileUtils.copyInputStreamToFile(inputStream, newFile);
if(inputStream!=null){
inputStream.close();
}
if(checkFileSize(file.getSize(),5,"M")){
System.out.println("照片不能大于5M");
zoomImage(path+"/"+fileName,path+"/"+fileName,5);
}
return 1;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
/*
* 图片按比率缩放
* size为文件大小
* src为源文件目录,dest为缩放后保存目录
*/
public static int zoomImage(String src,String dest,Integer size) throws Exception {
File srcFile = new File(src);
File destFile = new File(dest);
long fileSize = srcFile.length();
// if(fileSize < size * 1048576){
// //文件大于size M时,才进行缩放,注意:size以M为单位
// return -1;
// }
Double rate = (size * 1048576 * 0.5) / fileSize; // 获取长宽缩放比例
BufferedImage bufImg = ImageIO.read(srcFile);
Image Itemp = bufImg.getScaledInstance(bufImg.getWidth(), bufImg.getHeight(), bufImg.SCALE_SMOOTH);
AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(rate, rate), null);
Itemp = ato.filter(bufImg, null);
try {
ImageIO.write((BufferedImage) Itemp,dest.substring(dest.lastIndexOf(".")+1), destFile);
} catch (Exception ex) {
ex.printStackTrace();
}
return 1;
}
/**
* 多个文件上传
* @return -1、上传失败 1、上传成功
*/
public static String uploadMany(MultipartFile[] files,String fileName,String path){
InputStream inputStream = null;
String mobile = fileName;
try {
String fileUrl = "";
File newFile1 = new File(path);
if (!newFile1.exists()&& !newFile1.isDirectory()) {
newFile1.mkdir();
}
for (int i = 0; i < files.length; i++) {
String sname = files[i].getOriginalFilename();
String randomNumber = String.valueOf((new Random().nextInt(8999) + 1000));
sname = sname.substring(sname.lastIndexOf("."));
fileName = mobile+randomNumber+sname;
// System.out.println("------------------文件"+i+fileName);
// 获取后缀名
File file = new File(path+"/"+fileName);
inputStream = files[i].getInputStream();
FileUtils.copyInputStreamToFile(inputStream, file);
fileUrl += "/"+path.substring(path.indexOf("upload"),path.length())+"/"+fileName+",";
continue;
}
if(inputStream!=null){
inputStream.close();
}
if(!"".equals(fileUrl)){
fileUrl = fileUrl.substring(0,fileUrl.length()-1);
}
return fileUrl;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 单个文件下载
*
* @return -1、下载失败 1、下载成功
*/
public static int downSingle(String path, HttpServletResponse response) throws IOException {
File file = new File(path);
File[] files = file.listFiles();
String name = files[0].getName();//随机获取一个文件,实际中按需编写代码
System.out.println("文件的名字:"+name);
response.addHeader("content-disposition", "attachment;filename="+name);
FileUtils.copyFile(files[0], response.getOutputStream());
return 1;
}
/**
* 文件下载,一下次下载多个文件
* 思路:先将多个文件压缩到一个压缩包里去,然后传到前台
*
* @return -2、压缩包删成功 -1、压缩包产出失败 1、下载成功
*/
public static int downMany(String path,HttpServletResponse response) throws IOException{
File file = new File(path);
File[] files = file.listFiles();
File zipFile =new File("test.zip");
if(!zipFile.exists()){
zipFile.createNewFile();
}
String zipName = zipFile.getName();
System.out.print("压缩文件的名字:{}"+zipName);
response.addHeader("Content-Disposition", "attachment;filename="+zipName);
//定义输出类型
// response.setContentType("application/zip");
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFile));
BufferedInputStream in =null;
System.out.print("文件的个数{}"+files.length);
for(int i = 0;i<files.length;i++){
String name = files[i].getName();
System.out.println("文件的名字:"+name);
ZipEntry zipEntry = new ZipEntry(name);
zip.putNextEntry(zipEntry);
in = new BufferedInputStream(new FileInputStream(files[i]));
int len = 0;
byte [] btyes = new byte[1024*4];
while((len=in.read(btyes))!=-1){
zip.write(btyes, 0, len);
}
}
zip.flush();
zip.close();
in.close();
FileUtils.copyFile(zipFile, response.getOutputStream());
if(zipFile.exists()){
if(zipFile.delete()){
System.out.print("压缩包删成功!!");
return -2;
}else{
System.out.print("压缩包产出失败!!");
return -1;
}
}
return 1;
}
/**
* 判断文件大小
*
* @param len
* 文件长度
* @param size
* 限制大小
* @param unit
* 限制单位(B,K,M,G)
* @return
*/
public static boolean checkFileSize(Long len, int size, String unit) {
// long len = file.length();
double fileSize = 0;
if ("B".equals(unit.toUpperCase())) {
fileSize = (double) len;
} else if ("K".equals(unit.toUpperCase())) {
fileSize = (double) len / 1024;
} else if ("M".equals(unit.toUpperCase())) {
fileSize = (double) len / 1048576;
} else if ("G".equals(unit.toUpperCase())) {
fileSize = (double) len / 1073741824;
}
if (fileSize < size) {
return false;
}
return true;
}
}