關於ajax請求下載檔案的問題
阿新 • • 發佈:2019-01-10
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response1 = null;
String api = request.getParameter("api");
String responseJson = null;
if(api.equals("0")){
//獲得請求檔名
String fid = request.getParameter("fid");
String oid = request.getParameter("oid");
String num = request.getParameter("num");
String filename =new String(request.getParameter("filename").getBytes("iso8859-1"),"utf-8");
//設定響應頭,對檔案進行url編碼
filename = URLEncoder.encode(filename, "UTF-8");
System.out.println(filename);
String syspath = "C:/org_spaces";
String destination = syspath + "/" + oid + "/am/emltp/"+fid;
String suffix = filename.substring(filename.lastIndexOf("."));
String fname = fid + "_" + num+suffix;
String path = destination+"/"+fname;
//設定檔案MIME型別
response.setContentType(getServletContext().getMimeType(filename));
//設定Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//讀取目標檔案,通過response將目標檔案寫到客戶端
//System.out.println(fullFileName);
//讀取檔案
InputStream in = new FileInputStream(path);
OutputStream out = response.getOutputStream();
//寫檔案
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
out.close();
}
}
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response1 = null;
String api = request.getParameter("api");
String responseJson = null;
if(api.equals("0")){
//獲得請求檔名
String fid = request.getParameter("fid");
String oid = request.getParameter("oid");
String num = request.getParameter("num");
String filename =new String(request.getParameter("filename").getBytes("iso8859-1"),"utf-8");
//設定響應頭,對檔案進行url編碼
filename = URLEncoder.encode(filename, "UTF-8");
System.out.println(filename);
String syspath = "C:/org_spaces";
String destination = syspath + "/" + oid + "/am/emltp/"+fid;
String suffix = filename.substring(filename.lastIndexOf("."));
String fname = fid + "_" + num+suffix;
String path = destination+"/"+fname;
//設定檔案MIME型別
response.setContentType(getServletContext().getMimeType(filename));
//設定Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//讀取目標檔案,通過response將目標檔案寫到客戶端
//System.out.println(fullFileName);
//讀取檔案
InputStream in = new FileInputStream(path);
OutputStream out = response.getOutputStream();
//寫檔案
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
out.close();
}
}