檔案的上傳,下載,多個檔案生成壓縮包,檔案的刪除
阿新 • • 發佈:2019-02-17
檔案下載
/** * 檔案上傳 */ @RequestMapping("registerEmployee")
public String registerEmployee(String ename,String hiredate,MultipartFile photo,HttpServletRequest req) throws IllegalStateException, IOException{
//獲取檔名
String file_name=photo.getOriginalFilename();
//獲取檔案字尾名
String pre_file=FilenameUtils.getExtension(file_name);
//隨機生成名字
String base_name=UUID.randomUUID().toString();
file_name=base_name+"."+pre_file;
if(!photo.isEmpty()){
photo.transferTo(new File("d:\\pingbao\\"+file_name));
}
} /** * 生成下載檔案 * filename為要下載的檔名 */ public static void generateDownloadFile(String filename,HttpServletRequest request,HttpServletResponse response){ File file = new File(filename); try { InputStream ins = new FileInputStream(file); BufferedInputStream bins = new BufferedInputStream(ins); OutputStream outs = response.getOutputStream(); BufferedOutputStream bouts = new BufferedOutputStream(outs); response.setContentType("application/x-download"); response.setHeader("Content-disposition","attachment;filename="+URLEncoder.encode(file.getName(), "UTF-8")); int bytesRead = 0; byte[] buffer = new byte[8192]; // 開始向網路傳輸檔案流 while ((bytesRead =bins.read(buffer, 0, 8192)) != -1) { bouts.write(buffer, 0, bytesRead); } bouts.flush(); ins.close(); bins.close(); bouts.close(); }catch (IOException e) { e.printStackTrace(); } } 多個檔案打包
public static String multipleFilesToZip(String[] filename,String path){
// 隨機生成名字
String base_name = UUID.randomUUID().toString();
String fileZip = base_name + ".zip"; // 拼接zip檔案
String filepath = path+"\\" + fileZip;
File files[] = new File[filename.length];
for (int i = 0; i < filename.length; i++) {
files[i] = new File(filename[i]);
}
// 建立壓縮檔案
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filepath));
ZipOutputStream zos = new ZipOutputStream(bos);
ZipEntry ze = null;
for (int i = 0; i < files.length; i++) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(files[i]));
ze = new ZipEntry(files[i].getName());
zos.putNextEntry(ze);
int s = -1;
while ((s = bis.read()) != -1) {
zos.write(s);
}
bis.close();
}
zos.flush();
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
return filepath;
}
檔案刪除(多檔案)
/**
* 遞迴刪除目錄下的所有檔案及子目錄下所有檔案
* @param dir 將要刪除的檔案目錄
*/
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
//遞迴刪除目錄中的子目錄下
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目錄此時為空,可以刪除
return dir.delete();
}
/**
* 生成下載檔案
* filename為要下載的檔名
*/
/** * 檔案上傳 */ @RequestMapping("registerEmployee")
public String registerEmployee(String ename,String hiredate,MultipartFile photo,HttpServletRequest req) throws IllegalStateException, IOException{
//獲取檔名
String file_name=photo.getOriginalFilename();
//獲取檔案字尾名
String pre_file=FilenameUtils.getExtension(file_name);
//隨機生成名字
String base_name=UUID.randomUUID().toString();
file_name=base_name+"."+pre_file;
if(!photo.isEmpty()){
photo.transferTo(new File("d:\\pingbao\\"+file_name));
}
} /** * 生成下載檔案 * filename為要下載的檔名 */ public static void generateDownloadFile(String filename,HttpServletRequest request,HttpServletResponse response){ File file = new File(filename); try { InputStream ins = new FileInputStream(file); BufferedInputStream bins = new BufferedInputStream(ins); OutputStream outs = response.getOutputStream(); BufferedOutputStream bouts = new BufferedOutputStream(outs); response.setContentType("application/x-download"); response.setHeader("Content-disposition","attachment;filename="+URLEncoder.encode(file.getName(), "UTF-8")); int bytesRead = 0; byte[] buffer = new byte[8192]; // 開始向網路傳輸檔案流 while ((bytesRead =bins.read(buffer, 0, 8192)) != -1) { bouts.write(buffer, 0, bytesRead); } bouts.flush(); ins.close(); bins.close(); bouts.close(); }catch (IOException e) { e.printStackTrace(); } } 多個檔案打包