檔案上傳、下載

來源:互聯網
上載者:User

標籤:

  檔案下載比較簡單,response設定頭  告訴瀏覽器這是一個檔案下載  然後getOutputStream寫到指定地方去就行了

  如:

    resp.setHeader("Content-Disposition", "attachment;filename="+new String("測試".getBytes("GBK"),"ISO8859-1")+".jpg");

    InputStream in = this.getClass().getClassLoader().getResourceAsStream("1.jpg");

    OutputStream out = resp.getOutputStream();

    byte[] bytes = new byte[8192];
    int len = 0;
    while((len=in.read(bytes))!=-1){
      out.write(bytes, 0, len);
    }
    out.flush();
    in.close();
    out.close();

 

  檔案上傳也不麻煩  在jsp中添加一個file表單域   設定表單的enctype="multipart/form-data"  method必須為post

    需要匯入兩個包  在apache.org 下的Commons中  commons-fileupload-1.3.2.jar  commons-io-2.5.jar

    判斷請求是否為檔案上傳

    如果是建立解析器  建立臨時倉庫給解析器  把解析器交給檔案上傳對象

    解析請求

    判斷是否為普通表單域   如果是  普通執行  如果不是  檔案上傳處理

    具體詳見檔案上傳代碼

 

檔案上傳、下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.