SpringMVC上傳下載檔案
阿新 • • 發佈:2021-02-06
下載程式碼
//file 為真實檔案(從ftp或者資料庫中獲取)
try {
FileInputStream fins = new FileInputStream(file);
System.out.println(file.getName().getBytes());
response.setHeader("Content-disposition", "attachment; filename=" + new String((file.getName()).getBytes(), "ISO-8859-1" ));
response.setContentType("application/msexcel; charset=GB2312");
ServletOutputStream sopt = response.getOutputStream();
byte buffer[] = new byte[8192];
while (fins.read(buffer) != -1) {
sopt.write(buffer);
}
//關閉 清除
sopt.flush();
sopt.close( );
fins.close();
file.delete();//刪除快取檔案
} catch (Exception e) {
// TODO: handle exception
}
上傳
//通過ftp工具類將檔案上傳到ftp伺服器中
//param1為快取檔案地址
//param2為ftp的遠端地址
boolean boo = ftpUtils.putFile(localpath + "//" + filename, File.separator + remotepath);
//boo 為上傳結果
//ftp工具類使用的是下圖中的FileUtils(org.apache.commons.io.FileUtils)