หน้าเว็บ

วันอาทิตย์ที่ 18 พฤษภาคม พ.ศ. 2557

Servlet I/O Download File : java


maven dependencies (pom.xml)
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>16.0.1</version>
    <type>jar</type>
</dependency>
IO.java
package com.blogspot.na5cent.util;

import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.FileNameMap;
import java.net.URLConnection;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author redcrow
 */
public class IO {

    public static void download(HttpServletResponse response, InputStream inputStream, String fileName) throws IOException {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        OutputStream outputStream = null;
        try {
            response.setContentType(fileNameMap.getContentTypeFor(fileName));
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setHeader("language", "th-TH");

            outputStream = response.getOutputStream();
            ByteStreams.copy(inputStream, outputStream);
        } catch (IOException ex) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            throw ex;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }

            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}
Example to use
DownloadService.java
...
...
...
import com.blogspot.na5cent.util.IO;

/**
 *
 * @author redcorw
 */
@WebServlet(urlPatterns = "/downloadFile")
public class DownloadService extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        String path = request.getParameter("path");
        final File file = new File(UploadUtils.getUploadTempDirectory(), path);

        //use library
        IO.download(response, new FileInputStream(file), file.getName());
    }
}
Html
<a href="http://localhost:8080/test/downloadService?path=/folder/test_file.png"></a>



        ถ้า type ที่ dowload มาไม่ตรงตาม file type นั้นๆ ให้เพิ่ม code ต่อไปนี้ลงไปใน web.xml ครับ  เป็นการ map จาก extension file --> mime type file
<?xml version="1.0" encoding="UTF-8"?>
<web-app
...
...
..

    <!-- MS Word -->
    <mime-mapping>
        <extension>doc</extension>
        <mime-type>application/msword</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>dot</extension>
        <mime-type>application/msword</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>docx</extension>
        <mime-type>application/vnd.openxmlformats-officedocument.wordprocessingml.document</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>dotx</extension>
        <mime-type>application/vnd.openxmlformats-officedocument.wordprocessingml.template</mime-type>
    </mime-mapping>
    <!-- MS Word -->
    
    
    
    <!-- MS Excel -->
    <mime-mapping>
        <extension>xla</extension>
        <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>xlc</extension>
        <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>xlm</extension>
        <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>xls</extension>
        <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>xlt</extension>
        <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>xlw</extension>
        <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>xlsx</extension>
        <mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>xltx</extension>
        <mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.template</mime-type>
    </mime-mapping>
    <!-- MS Excel -->
    
    
    
    <!-- MS Powerpoint -->
    <mime-mapping>
        <extension>pot,</extension>
        <mime-type>application/vnd.ms-powerpoint</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>pps</extension>
        <mime-type>application/vnd.ms-powerpoint</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>ppt</extension>
        <mime-type>application/vnd.ms-powerpoint</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>pptx</extension>
        <mime-type>application/vnd.openxmlformats-officedocument.presentationml.presentation</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>ppsx</extension>
        <mime-type>application/vnd.openxmlformats-officedocument.presentationml.slideshow</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>potx</extension>
        <mime-type>application/vnd.openxmlformats-officedocument.presentationml.template</mime-type>
    </mime-mapping>
    <!-- MS Powerpoint -->
    
    
    
    <!-- MS Project -->
    <mime-mapping>
        <extension>mpp</extension>
        <mime-type>application/vnd.ms-project</mime-type>
    </mime-mapping>
    <!-- MS Project -->
    
    
    
    <!-- PDF -->
    <mime-mapping>
        <extension>pdf</extension>
        <mime-type>application/pdf</mime-type>
    </mime-mapping>
    <!-- PDF -->
    
    
    
    <!-- Zip -->
    <mime-mapping>
        <extension>zip</extension>
        <mime-type>application/zip</mime-type>
    </mime-mapping>
    <!-- Zip -->
    
    
    
    <!-- image -->
    <mime-mapping>
        <extension>jpe</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>jpeg</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>jpg</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>ico</extension>
        <mime-type>image/x-icon</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>gif</extension>
        <mime-type>image/gif</mime-type>
    </mime-mapping>
    
    <mime-mapping>
        <extension>png</extension>
        <mime-type>image/png</mime-type>
    </mime-mapping>
    <!-- image -->
...
...
...
</web-app>

ไม่มีความคิดเห็น:

แสดงความคิดเห็น