1. 程式人生 > >SSH 文件下載 代碼跟註意點

SSH 文件下載 代碼跟註意點

nco utf-8 exists pfile available eset spl set header

該段代碼實現了壓縮文件,並下載到本地。註意事項:不能在ajax中彈出下載框,否則chrome可以下載。但是IE閃退!下載需要response的輸出流寫入操作

前臺代碼:// 創建文件夾
function makeDirs(){
if(ajList.length==0){
alertMsg("請添加打印列表");
return false;
}
window.open(‘${pageContext.request.contextPath}/business/dajg_dkb.do?makeDirByGdhsMore&ids=‘+ajList+"&fwlxid="+fwlxid[0]+"&tableName="+tableName);

}

後臺代碼:

@RequestMapping(params = "makeDirByGdhsMore" )
@ResponseBody
public void makeDirByGdhsMore(String ids ,String fwlxid ,String tableName,HttpServletResponse response){
SuccessMsg successMsg = new SuccessMsg();
try {
successMsg = dkbdaAjService.dirByGdhsMore(ids,fwlxid,tableName);
Map <String,Object> map =new HashMap<String,Object>(2);
if(successMsg.isSuccess()){
String pathBase = ResourceUtil.getResource("dbconfig.properties").get("dirpath");
pathBase = pathBase.replace("/","\\");
HttpSession session = ContextHolderUtil.getRequest().getSession();
SessionUser sessionUser=(SessionUser)session.getAttribute("login_session_user");
String name = sessionUser.getUserName(),zipPath =pathBase+"-"+name+"-"+count+".zip";

CompressUtil.zipNoCryption(pathBase,zipPath);
count++;
for(int i=0;i<3;i++){
CompressUtil.deleteAllDirs(new File(pathBase),pathBase);
}
zipPath = zipPath.replace("\\","/").split("/")[1];
map.put("dirName",zipPath);
successMsg.setDataMap(map);
successMsg.setMsg(successMsg.getMsg()+" 壓縮成功!");

getFile(zipPath,response);

}
} catch (Exception e) {
successMsg.setMsg(e.getMessage());
successMsg.setSuccess(false);
e.printStackTrace();
}
}

public void getFile(String fileName, HttpServletResponse response){

InputStream in =null;
File file= null;
try {
file=new File("D:\\"+fileName);
if(file.exists()){
System.out.println(file.getName());
}
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
in=new FileInputStream(file);
FileUtil.downLoadZipFile("D:\\"+fileName,fileName, response);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
in.close();
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
}

}

/**
* 下載文件
* @param filePath
* @param fileName
* @param response
*/
public static HttpServletResponse downLoadZipFile(String filePath,String fileName,HttpServletResponse response){
File file=new File(filePath);
if(!file.exists())
return null ;
return download(filePath,fileName,response);
}
public static HttpServletResponse download(String path, String fileName, HttpServletResponse response) {
try {
// path是指欲下載的文件的路徑。
File file = new File(path);
try{
fileName = URLEncoder.encode(fileName, "UTF-8");
}catch (Exception e){
e.printStackTrace();
}

// 以流的形式下載文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 設置response的Header
response.setContentType("application/octet-stream; charset=utf-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}

SSH 文件下載 代碼跟註意點