日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java的jsp要下载吗_jsp、java下载附件

發布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java的jsp要下载吗_jsp、java下载附件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 傳入此jsp中的參數均已URLDencoder過。2

3

4

5

6

7 boolean isError = false;8 String errorMsg = "";9 response.reset();//可以加也可以不加

10 request.setCharacterEncoding("UTF-8");11 String folder = "news";12 if(request.getParameter("folder")!=null){13 folder = "upload/"+(String)request.getParameter("folder");14 }15 String newname = request.getParameter("newname");16 //是否允許直接在瀏覽器內打開

17 boolean isInline = false;18 out.clear();19 response.reset();20 String filePath = request.getRealPath("/"+folder)+"/" +newname;21 java.io.BufferedOutputStream output = null;22 java.io.BufferedInputStream input = null;23 try{24 java.io.File f = newjava.io.File(filePath);25 if(f.exists() &&f.canRead()){26 String mimetype = null;27 mimetype =application.getMimeType( filePath );28 if(mimetype==null){29 mimetype = "application/octet-stream;charset=utf-8";30 }31 response.setContentType(mimetype);32 //IE 的話就只能用 IE 才認識的頭才能下載 HTML 文件, 否則 IE 必定要打開此文件!

33 String ua = request.getHeader("User-Agent"); //獲取終端類型

34 if(ua==null ){ua = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0;)";}35 boolean isIE = ua.toLowerCase().indexOf("msie")!=- 1; //是否為 IE

36 if (isIE && !isInline) {37 mimetype = "application/x-msdownload";38 }39 response.setContentType(mimetype);40 //是否內聯附件

41 String inlineType = isInline?"inline":"attachment";42 response.setHeader("Content-Disposition" , inlineType + ";filename=" +filename);43 //設置下載內容大小

44 response.setContentLength((int)f.length());45 //緩沖區

46 byte[] buffer = new byte[ 4096];47 output = newjava.io.BufferedOutputStream(response.getOutputStream());48 input = new java.io.BufferedInputStream(newjava.io.FileInputStream(f));49 int n = (-1);50 while((n=input.read(buffer,0,4096))>-1){51 output.write(buffer,0,n);52 }53 response.flushBuffer();54

55 }else{56 isError = true;57 errorMsg = "文件不存在!";58 }59 } catch(Exception ex) {60 isError = true;61 errorMsg = "您下載的文件出現異常!";62 } finally{63 if(isError){64 String path = request.getHeader("Referer");65 errorMsg=new String(errorMsg.getBytes("GBK"), "ISO8859_1");66 %>

67

68 alert('');69 window.location.href='';70

71

72 }73 if(input!=null){input.close();}74 if(output!=null){output.close();}75 out.clear();76 out =pageContext.pushBody();77 }78 %>

備注:

public void downBatchRateModelFile(String fileName,String downName, HttpServletResponse response,HttpServletRequest request) {

try {

String downPath = TemplateBuilder.getInstance().getBatchRateModelFold()+File.separator+fileName;

// path是指欲下載的文件的路徑。

File file = new File(downPath);

// 取得文件名。

String filename = file.getName();

// 取得文件的后綴名。

String ext = filename.substring(filename.lastIndexOf(".") )

.toLowerCase();

filename = downName+ext;

// 以流的形式下載文件。

InputStream fis = new BufferedInputStream(new FileInputStream(downPath));

byte[] buffer = new byte[fis.available()];

fis.read(buffer);

fis.close();

// 清空response

response.reset();

// 設置response的Header

response.addHeader("Content-Length", "" + file.length());

String mimetype = "application/octet-stream;charset=utf-8";

String ua = request.getHeader("User-Agent"); // 獲取終端類型

if (ua == null) {

ua = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0;)";

}

boolean isIE = ua.toLowerCase().indexOf("msie") != -1; // 是否為 IE

if (isIE) {

mimetype = "application/x-msdownload";

response.addHeader("Content-Disposition", "attachment;filename="

+ java.net.URLEncoder.encode(filename, "UTF-8"));

}else{

response.addHeader("Content-Disposition", "attachment;filename="

+ new String(filename.getBytes("GBK"),"ISO-8859-1"));

}

response.setContentType(mimetype);

//response.setContentType("application/x-msdownload");

OutputStream toClient = new BufferedOutputStream(

response.getOutputStream());

//response.setContentType("application/octet-stream");

toClient.write(buffer);

toClient.flush();

toClient.close();

} catch (IOException e) {

}

}

總結

以上是生活随笔為你收集整理的java的jsp要下载吗_jsp、java下载附件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。