Sometimes while doing programming, I need to download some jars, but the network settings blocks it. That is really embarrassing So I came with a java program which downloads file for me using third party server.  To implement this program you need to download apache-commons-io library. This program is simple servlet.

Program flow

  1. Get file URL from user
  2. Send it to proxy servlet
  3. Servlet will open input stream for that URL and write content to servlet output stream
  4. User will get file downloaded

Get file URL from user

String fileURL = request.getParameter("url");

Open Input Stream 

URL url = new URL(fileURL);
InputStream is = url.openStream();

Write Into Output stream

ServletOutputStream out = response.getOutputStream();
out.write(IOUtils.toByteArray(is));
out.flush();
out.close()

Complete Servlet Code

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;


/**
 * Servlet implementation class ProxyFile
 */
public class ProxyFile extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProxyFile() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // get file name from parameter
        String fileURL = request.getParameter("url");
        URL url;
        InputStream is = null;
        try {
            // get name of the file
            String[] names = fileURL.split("/");
            String fileName = names[names.length - 1];
            
            // open URL stream
            url = new URL(fileURL);
            is = url.openStream();
            
            // set response header
            response.setContentType("application/download");
            response.setHeader("Content-Transfer-Encoding", "binary");
            response.setHeader("Content-Disposition","attachment; filename=\"" + fileName+"\"");
            
            // write into servlet output stream  
            ServletOutputStream out = response.getOutputStream();
            out.write(IOUtils.toByteArray(is));
            out.flush();
            out.close();
        } catch (MalformedURLException mue) {
            mue.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (IOException ioe) {
            }
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // forward to doGet
        doGet(request, response);
    }

}

web.xml Configuration

  <servlet>
    <description></description>
    <display-name>ProxyFile</display-name>
    <servlet-name>ProxyFile</servlet-name>
    <servlet-class>demo.sl.ProxyFile</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ProxyFile</servlet-name>
    <url-pattern>/ProxyFile</url-pattern>
  </servlet-mapping>

0 comments:

Blogroll

Follow this blog by Email

Popular Posts