REST下载期间取消文件传输

时间:2014-03-14 21:47:33

标签: java rest ftp gateway

当我尝试下载文件时,我遇到了一些问题。

这是我的功能:

@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;
}

客户端的getFile函数使用libapache方法:

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);
    }
}

当我尝试下载我的网络浏览器时说下载已取消: Response Header

我真的不知道我做错了什么,网上没有任何东西帮助我到目前为止(已经有2个小时......)

1 个答案:

答案 0 :(得分:0)

我发现问题是什么,FTP传输应该是二进制模式..