1. 程式人生 > >java瀏覽器檔案下載和圖片顯示(流形式)

java瀏覽器檔案下載和圖片顯示(流形式)

瀏覽器檔案下載:如果你想在瀏覽器中實現下載功能--一些原本不預設下載的檔案,如:jpg、xml等。

圖片顯示(流形式):如果你想在瀏覽器中顯示圖片,而圖片不是一個靜態檔案(沒有url地址)

那我們應該怎麼做呢?

分析:

瀏覽器獲得檔案是通過http協議的,

所以只要我設定好請求(request)返回的響應(response)的一些資訊應該就行了,

那就是設定響應(response)頭的一些資訊嘍。

解決:

瀏覽器檔案下載設定:

Content-Type:application/octet-stream           // 未分類的二進位制資料

Content-Disposition:attachment;filename=yourFileName  //附件形式處理,檔名為yourFileName

Content-Length:yourFile.length               //檔案的大小

而檔案以流形式輸出為瀏覽器就行了。

這樣瀏覽器就能識別該檔案是通過附件的形式下載的了。

java的servlet程式碼:

複製程式碼
public HttpServletResponse getFile(String path,HttpServletRequest request, HttpServletResponse response) {
        try {
            File file = new File(request.getRealPath("/")+"/"+path);
String filename = file.getName(); InputStream fis = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); // 設定response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"),"ISO-8859-1")); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (IOException ex) { ex.printStackTrace(); } return response; }
複製程式碼

 java的restlet主要程式碼:

複製程式碼
            final byte[] bpmnBytes = //檔案流位元組;
            Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
            disposition.setFilename(fileName);
            OutputRepresentation output = new OutputRepresentation(org.restlet.data.MediaType.APPLICATION_OCTET_STREAM) {
                public void write(OutputStream os)
                        throws IOException
                {
                    os.write(bpmnBytes);
                    os.flush();
                }
            };
            output.setDisposition(disposition);
            return output;
複製程式碼

圖片(流形式)顯示設定:

Content-Type:image/jpeg                // jpeg、jpg、jpe、jfif形式的圖片

Content-Length:yourImg.length              //圖片大小

而圖片以流形式輸出為瀏覽器就行了。

java的servlet程式碼:

複製程式碼
public HttpServletResponse getImage(String path,HttpServletRequest request, HttpServletResponse response) {
        try {
            File file = new File(request.getRealPath("/")+"/"+path);
            String filename = file.getName();

            InputStream fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();

            response.reset();
            // 設定response的Header            
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("image/jpeg");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
    }
複製程式碼

 java的restlet主要程式碼:

複製程式碼
                final byte[] result = //圖片流位元組;

                return new OutputRepresentation(MediaType.IMAGE_PNG) {
                    public void write(OutputStream os)
                            throws IOException
                    {
                        os.write(result);
                        os.flush();
                        os.close();
                    }
                };
複製程式碼

這樣瀏覽器就能正確識別該圖片,並在瀏覽器中識別出來。

當然也可以是img標籤的src中顯示。

原文地址:http://www.cnblogs.com/wxxian001/archive/2013/04/24/3040648.html