You can try returning a StreamingOutput object instead of the FileInputStream 
and in the StreamingOutput#write method, copy the bytes from the 
FileInputStream to the OutputStream.

http://commons.apache.org/io/apidocs/org/apache/commons/io/IOUtils.html might 
help. Once you are done copying the bytes, delete the file.

On Sep 1, 2012, at 10:47 AM, Rohit Singh <[email protected]> wrote:

> Hello,
> 
> I am implement a API that lets user download file.
> 
> This file is created for each user and can be different for each request.
> 
> So, I have to delete the file when a user is done downloading the file. The 
> files size can be in Gigs, so the download wont be quick.
> 
> We are using wink for rest of the API, this is just a extension. So I will 
> not like to keep implementation as is in same file.
> 
> My question is how do I know that the download operation is finished, so I 
> can delete the file.
> 
> 
> Here is a test case, similar to what I do in the real implementation.
> 
> 
> import java.io.File;
> import java.io.FileNotFoundException;
> import java.io.FileReader;
> import java.io.IOException;
> import java.io.LineNumberReader;
> import java.io.FileInputStream;
> import java.io.InputStream;
> import javax.ws.rs.GET;
> import javax.ws.rs.HeaderParam;
> import javax.ws.rs.Path;
> import javax.ws.rs.PathParam;
> import javax.ws.rs.Produces;
> import javax.ws.rs.core.Context;
> import javax.ws.rs.core.HttpHeaders;
> import javax.ws.rs.core.MediaType;
> import javax.ws.rs.core.Response;
> import javax.ws.rs.core.Response.ResponseBuilder;
> 
> @Path("fileDownLoader")
> public class FileDownLoader {
>       private final String logpath = "C:\\temp\\";
> 
>       @Context
>       HttpHeaders requestHeaders;
> 
>       @GET
>       @Produces({"application/json"})
>       @Path("/{fileName}")
>       public Response downLoadFile(@PathParam("fileName") String fileName) {
> 
>         Response response = null;
>               InputStream in = null;
>               try {
>               in = new FileInputStream(logpath+fileName);
>               response = Response.ok(in, 
> MediaType.APPLICATION_OCTET_STREAM_TYPE).build();
>               } catch( FileNotFoundException fnfe) {
>               fnfe.printStackTrace();
>               }
> 
>               return response;
> 
>   }
> }
> 
> -- 
> -- 
> Rohit
> 
> “We all die. The goal isn't to live forever, the goal is to create something 
> that will.” Chuck Palahniuk
> 
> http://www.facebook.com/huskercane
> 

Reply via email to