java Upload and download

來源:互聯網
上載者:User

標籤:

/*** Upload comoms-fileupload*/        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();        ServletFileUpload sfu = new ServletFileUpload(diskFileItemFactory);                List<FileItem> fileItemList = null;        try {            fileItemList = sfu.parseRequest(request);        } catch (FileUploadException e) {            e.printStackTrace();        }        FileItem fi0 = fileItemList.get(0);        FileItem fi1 = fileItemList.get(1);                response.getWriter().println(fi0.getFieldName() + ":" + fi0.getString());        response.getWriter().println(fi1.getName() + ":" + fi1.getSize());        try {            fi1.write(new File("c:/haha.jpg"));        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        /*** Download* 兩個頭一個流*/    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        String filename = "D:/Download/the first time.rmvb";        String ext = filename.substring(filename.lastIndexOf("."));        String disposition = "attachment;filename="+UUID.randomUUID()+ext;        String mineType = this.getServletContext().getMimeType(filename);        FileInputStream fis = new FileInputStream(filename);        response.setHeader("Content-Type", mineType);        response.setHeader("Content-Disposition", disposition);        ServletOutputStream output = response.getOutputStream();        IOUtils.copy(fis, output);        fis.close();    }

 

/*** download中文檔案名稱字亂碼問題*/package cn.itcast.down.utils;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import javax.servlet.http.HttpServletRequest;import sun.misc.BASE64Encoder;public class DownUtils {    public static String filenameEncoding(String filename, HttpServletRequest request) throws IOException {        String agent = request.getHeader("User-Agent"); //擷取瀏覽器        if (agent.contains("Firefox")) {            BASE64Encoder base64Encoder = new BASE64Encoder();            filename = "=?utf-8?B?"                    + base64Encoder.encode(filename.getBytes("utf-8"))                    + "?=";        } else if(agent.contains("MSIE")) {            filename = URLEncoder.encode(filename, "utf-8");        } else {            filename = URLEncoder.encode(filename, "utf-8");        }        return filename;    }}

 

java Upload and download

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.