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>

7 comments:

  1. If you’re considering building it solutions a mobile app for your small to medium-sized business, you need to know exactly what it takes to execute.

    ReplyDelete
  2. We suggest you purchase your first or update it in your wardrobe. You’ll have questions in your mind like: Does this fit my character? Or it works with things that I already have in my closet? What does quality resemble in leather stuff? By what method would it be advisable for it to fit? If you want correct answers to these inquiries, you’ll be the glad new buyer of a marvelous bit of menswear. Leather outfits for men are significant showcase stuff so try to teach yourself appropriately before purchasing your first WWE Mens Jackets

    ReplyDelete
  3. this blog is so helpful and help me to find a solution really amazing
    letsavebig



    ReplyDelete
  4. Dunkin' Donuts
    is a donut and coffee shop chain with locations all over the world. It also sells sandwiches, hash browns, and wake-up wraps in addition to more than 35 types. dunkin donuts coupons

    ReplyDelete
  5. Celebrate your mother's special day in such a significant style with the suggested gifts below for her 50th birthday. Making your mother happy is such a heartfelt feeling. Nothing can beat that, so click here.
    visit: 50th birthday gift ideas for mom

    ReplyDelete
  6. Searching for Youtube Tv Promo Code? Youtube TV is your new (not so new) live streaming platform that works on a subscription basis. You can broadcast live sports, channels, entertainment, movies, shows and all sorts of content in high quality.

    ReplyDelete
  7. particle size and particle size distribution - T,C&A LAB is a branch of Alfa Chemistry, headquartered in New York, USA. Today, T,C&A LAB is an independent lab providing quality or custom testing, characterization and analysis of a variety of materials. We can help with troubleshooting, R&D screening, raw materials retest and diagnosis, etc., to most industries worldwide. We can also assist clients in testing data interpretation and simulation by advanced data analysis techniques.

    ReplyDelete

Blogroll

Popular Posts