commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nico Krijnen <n...@everyage.com>
Subject RE: FileUpload question
Date Fri, 25 Apr 2003 14:08:29 GMT
> Is it possible to simply stream the file contents to the response?
Yes, streaming files is actualy very easy.

The best way is not to use a jsp page but a simple servlet:

public class MyFileServlet extends HttpServlet
{
	public void doPost(HttpServletRequest request, HttpServletResponse
response)
		throws ServletException, IOException
	{
		doGet(request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse
response)
		throws ServletException, IOException
	{
		BufferedInputStream in;
		response.setContentType(...);
		response.setContentLength(...);
		try
		{
			in = new BufferedInputStream(new
FileInputStream(...));
			OutputStream out = response.getOutputStream();
			int fileByte;
			while( (fileByte = in.read()) != -1 ) {
				out.write(fileByte);
			}
			out.flush();

		} catch (IOException e) {
			// Download was interupted
		} finally { // ???
			if (in != null) in.close();
		}
	}
}

Make sure you set the mime-type of the response using
'setContentType(String)'. That way the browser knows it is not dealing with
a html page.
After the content type has been set, you can use the 'getOutputStream()'
method to get the servlets output stream and write your file to it.
I don't know how to get an InputStream to the file data in your repository
so I used a FileInputStream as example here.

That should do it!

Nico Krijnen
Every Age Multimedia


-----Oorspronkelijk bericht-----
Van: Ross Allard [mailto:Ross.Allard@sas.com]
Verzonden: vrijdag 25 april 2003 15:43
Aan: commons-user@jakarta.apache.org
Onderwerp: FileUpload question


This is probably a basic question, but once I've gotten a file to the
mid-tier (and beyond), how do I deliver it back to a user when requested?  I
have a struts web application that needs to handle attachments.  I can use
fileupload to get the file from the client into a repository.  What's the
process of delivering it back to the user for viewing, in such a way that
the browser has enough information to know what to do? Is it possible to
simply stream the file contents to the response?  

Any help will be appreciated.

Ross Allard
Sas Institute

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org

Mime
View raw message