Search code examples
javarestftpgateway

File transfer is canceled during REST download


I have a little problem when I try to download a file with rest.

Here is my function :

@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("{directory: (([^/]+[/])+)[^/]+}")
public Response getFile(@PathParam("directory") String directory)
{

    Response responseFile = null;
    try
    {
        /*String tmpFileName = "REST_FTP_TMPFile_"+ Math.random();
               tmpFileName = tmpFileName.replace(".", "");

        File tmpFile = new File(tmpFileName);
        tmpFile.createNewFile();
        //*/

        String filename = directory.substring(directory.lastIndexOf("/")+1, directory.length());
        File tmpFile = File.createTempFile("REST_FTP_TMPFile_", null);

        directory = StringEscapeUtils.unescapeHtml4(directory);

        this.client.getFile(directory, tmpFile);

        System.out.println("size : " + tmpFile.length());            

        responseFile = Response.ok(tmpFile, MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-length", tmpFile.length())
                .header("Content-Disposition","attachment; filename="+filename+"")
                .build();

        //A voir quand on peut le supprimer...
        //tmpFile.delete();
    }
    catch (IOException ex)
    {
        Logger.getLogger(GetResource.class.getName()).log(Level.SEVERE, null, ex);
    }
    return responseFile;
}

The function getFile of client use the libapache method :

public boolean getFile(String pathname, File file) throws IOException
{
    OutputStream output;
    output = new FileOutputStream(file);

    System.out.println("Wanted pathname : "+pathname);

    boolean status = this.client.retrieveFile("/"+pathname, output);
    if(status)
    {
        output.close();
        return status;
    }
    else
    {
        output.close();
        throw new IOException("Cannot retrieve the file : " + pathname);
    }
}

When I try to download my web browser says that the download is canceled : Response Header

I really don't know what I did wrong and nothing on the web helped me out so far (it's been 2 hours..)


Solution

  • I found what the problem was, the FTP transfer is supposed to be in binary mode..