欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
JSP實(shí)現文件下載的幾種方式

JSP文件下載的幾種方式

1。最直接最簡(jiǎn)單的,方式是把文件地址直接放到html頁(yè)面的一個(gè)鏈接中。這樣做的缺點(diǎn)是把文件在服務(wù)器上的路徑暴露了,并且還無(wú)法對文件下載進(jìn)行其它的控制(如權限)。這個(gè)就不寫(xiě)示例了。
2。在服務(wù)器端把文件轉換成輸出流,寫(xiě)入到response,以response把文件帶到瀏覽器,由瀏覽器來(lái)提示用戶(hù)是否愿意保存文件到本地。(示例如下)
<%
 response.setContentType(fileminitype);
 response.setHeader(
"Location",filename);
 response.setHeader(
"Cache-Control""max-age=" + cacheTime);
 response.setHeader(
"Content-Disposition""attachment; filename=" + filename); //filename應該是編碼后的(utf-8)
 response.setContentLength(filelength);
 OutputStream outputStream 
= response.getOutputStream();
 InputStream inputStream 
= new FileInputStream(filepath);
 
byte[] buffer = new byte[1024];
 
int i = -1;
 
while ((i = inputStream.read(buffer)) != -1) {
  outputStream.write(buffer, 
0, i);
  }
 outputStream.flush();
 outputStream.close();
 inputStream.close();
 outputStream 
= null;

%>

3。既然是JSP的話(huà),還有一種方式就是用Applet來(lái)實(shí)現文件的下載。不過(guò)客戶(hù)首先得信任你的這個(gè)Applet小程序,由這個(gè)程序來(lái)接受由servlet發(fā)送來(lái)的數據流,并寫(xiě)入到本地。
servlet端示例
    public void service(HttpServletRequest req, HttpServletResponse res)
            
throws ServletException, IOException {
        res.setContentType(
" text/plain ");
        OutputStream outputStream 
= null;
        
try {
            outputStream 
= res.getOutputStream();
            popFile(srcFile, outputStream)) ;
//把文件路徑為srcFile的文件寫(xiě)入到outputStream中。
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 

JApplet端示例
   URLConnection con;
        
try {
            con 
= url.openConnection();//url是被調用的SERVLET的網(wǎng)址 如http://localhost:8080/sendDateSevlet.do  
            con.setUseCaches(false);
            con.setDoInput(
true);
            con.setDoOutput(
true);
            con.setRequestProperty(
"Content-Type",
                
"application/octet-stream");
            InputStream in 
= con.getInputStream();
            ProgressMonitorInputStream pmInputStream 
= new ProgressMonitorInputStream(
                    pane, 
"正在從服務(wù)器下載文件內容", in);
            ProgressMonitor pMonitor 
= pmInputStream
                    .getProgressMonitor();
            pMonitor.setMillisToDecideToPopup(
3);
            pMonitor.setMillisToPopup(
3);
            String localfilepath 
= localstr + filename ;//localfilepath本地路徑,localstr文件文件夾,filename本地文件名
     if(saveFilsaveFilee(localfilepath,pmInputStream)){ //方法saveFilsaveFilee是把輸入流pmInputStream寫(xiě)到文件localfilepath中。                    
     openLocalFile(localfilepath);
            }



4。順便把JApplet上傳文件的代碼也貼上來(lái).
JApplet端示例

URLConnection con;
        
try {
            con 
= url.openConnection();//url是被調用的SERVLET的網(wǎng)址 如http://localhost:8080/sendDateSevlet.do         
     con.setUseCaches(false);
            con.setDoInput(
true);
            con.setDoOutput(
true);
            con.setRequestProperty(
"Content-Type",
                
"application/octet-stream");
            
            OutputStream out 
= con.getOutputStream();
            String localfilepath 
= localstr + filename; //localfilepath本地路徑,localstr文件文件夾,filename本地文件名
            getOutputStream(localfilepath,out);//文件getOutputStream是把文件localfilepath寫(xiě)到輸出流out中。
            InputStream in = con.getInputStream();
            
return true;
        }
catch (IOException e) {
               System.out.println(
"文件上傳出錯!");
            e.printStackTrace();
        }

servlet端代碼示例
    public void service(HttpServletRequest req, HttpServletResponse res)
            
throws ServletException, IOException {
        res.setContentType(
" text/plain ");
        InputStream inputStream 
= null;
        
try {
            inputStream 
= res.getInputStream();
            writefile(srcFile, inputStream);
//把輸入流inputStream保存到文件路徑為srcFile的文件中
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 
// end service

 總結:在文件的傳輸中是流的形式存在的,在硬盤(pán)上是文件的形式存在的。我們要做的只是通過(guò)HttpServletRequest和HttpServletResponse,或者是response和request來(lái)發(fā)送流和讀取流。以及把文件轉換成流或把流轉換成文件的操作。
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
hibernate 下不適用映射文件來(lái)保存blob圖片
Java實(shí)例:利用java多線(xiàn)程斷點(diǎn)續傳實(shí)踐
struts2.2.1 文件上傳
Java IO的一般使用原則
節點(diǎn)流和處理流
Java I/O流-總結(InputStream,OutputStream,Reader,Writer)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久