public static void downloadFile(String fileUrl, String savePath) throws IOException {
URL url = new URL(fileUrl);
InputStream in = url.openStream();
FileOutputStream out = new FileOutputStream(savePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
in.close();
out.close();
}
public Map<String, Object> attachmentRecordDown(Map<String, Object> params) {
Map<String,Object>map = new HashMap<>();
String files = MapUtils.getString(params,"files");
if (StringUtils.isBlank(files)){
throw new CustomException("1","文件地址不能为空!");
}
String[] split = files.split(",");
if (split.length<1){
throw new CustomException("1","文件地址不能为空!");
}
File fileZip = new File(path+"/zip");
//如果已经存在则删除
if (fileZip.exists()) {
fileZip.delete();
}
//检查是否存在
if (!fileZip.exists())
{
fileZip.mkdirs();
}
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String dateStr = df.format(new Date());
List<String> paths = new ArrayList<>();
for (String s:split){
s = downPath+s;
String fileName1=s.substring(s.lastIndexOf("/")+1);
String filePaths = fileZip+"/"+fileName1;
try {
URL url = new URL(s);
InputStream fis = url.openStream();
BufferedInputStream bis =new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(filePaths);
BufferedOutputStream bos =new BufferedOutputStream(fos);
int read;
while ((read = bis.read())!=-1){
bos.write(read);
}
bis.close();
bos.close();
fis.close();
fos.close();
paths.add(filePaths);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
compress(paths,fileZip+"/"+dateStr+".zip");
} catch (IOException e) {
e.printStackTrace();
}
map.put("path","/localFile/zip/"+dateStr+".zip");
return map;
}
public static int compress(List<String> filePaths, String zipFilePath) throws IOException{
// 压缩下载保存的路径
File zipFile = new File(zipFilePath);
//设置压缩流:直接写入response,实现边压缩边下载
ZipOutputStream zipOs = null;
//循环将文件写入压缩流
DataOutputStream os = null;
//文件
File file;
try {
zipOs = new ZipOutputStream(new FileOutputStream(zipFile));
//设置压缩方法
zipOs.setMethod(ZipOutputStream.DEFLATED);
for (String path : filePaths) {
String fileName=path.substring(path.lastIndexOf("."));
fileName=path.substring(path.lastIndexOf("/")+1);
file = new File(path);
if (!file.exists()) {
throw new RuntimeException("文件不存在");
}
//添加ZipEntry,并将ZipEntry写入文件流
zipOs.putNextEntry(new ZipEntry(fileName));
os = new DataOutputStream(zipOs);
FileInputStream fs = new FileInputStream(file);
byte[] b = new byte[100];
int length;
//读入需要下载的文件的内容,打包到zip文件
while ((length = fs.read(b)) != -1) {
os.write(b, 0, length);
}
//关闭流
fs.close();
zipOs.closeEntry();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流
for (String str:filePaths){
File fileExcel = new File(str);
//如果已经存在则删除
if (fileExcel.exists()) {
fileExcel.delete();
}
String substring = str.substring(0, str.lastIndexOf("/"));
fileExcel = new File(substring);
// 判断该temp对象是否为目录对象
if (fileExcel.isDirectory()) {
fileExcel.delete();
}
}
try {
if (os != null) {
os.flush();
os.close();
}
if (zipOs != null) {
zipOs.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return 0;
}