Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: Re: Download Image using servlet!


Message #1 by "Jan Lichtarowicz" <jan.lichtarowicz@c...> on Fri, 9 Feb 2001 10:21:02
> I would like to download image frm web page is there any clue? 

Hi.
The solution to this depends on what you whant.

The simple solution is to open the file, read it into memmory, set the 
context type, and output to the client.

I have attached some code to do this from a form request so feel free to 
modify it as you need.
All the form does is sends the path to the file to be downloaded.
You may also wish to change the content type from text/rtf to image/gif or 
some other image.

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DownloadServlet extends HttpServlet
{

	private File 		_fin;
	private FileReader 	_in=null;
	private BufferedReader	_br = null;

	private String 		temp="";
	private String 		_strIn="";

	public void doPost(HttpServletRequest req, HttpServletResponse 
res) throws ServletException, IOException
	{

		String file = req.getParameter("file");

		// open file and buffers
		try
		{
			_fin=new File(file);

			_in=new FileReader(_fin);
			_br=new BufferedReader(_in);

		}
		catch(FileNotFoundException e)
		{
			System.out.println("File not found");
		}

		// read in file
		try
		{
			while((temp=_br.readLine())!=null)
			{
				_strIn= _strIn + temp + "\n";
			}
		}
		catch (IOException e)
		{
					System.out.println("Error reading 
file");
		}


		// output file to output
		res.setContentType("text/rtf");
		PrintWriter out = res.getWriter();


		out.print(_strIn);
		out.flush();

	}
}

  Return to Index