Web services 无法使用RESTEasy Web服务从Java HTTPClient(而不是浏览器)下载文件

Web services 无法使用RESTEasy Web服务从Java HTTPClient(而不是浏览器)下载文件,web-services,rest,Web Services,Rest,我一直在寻找解决办法,但到目前为止运气不好 要求: 我需要从文件系统挂载下载一个文件,例如sample.jpg,该请求必须从我的项目应用程序中调用。因此,我构建了一个web服务来实现这一点,如果我直接在浏览器上调用web服务,它的工作状态很好。i、 例如,如果我通过如下所示的浏览器URL调用该服务,则该文件将在我系统的浏览器默认下载位置下载 例如: 网络服务代码: @Path("/downloadservice") public class DownloadFileWS { @POST

我一直在寻找解决办法,但到目前为止运气不好

要求: 我需要从文件系统挂载下载一个文件,例如sample.jpg,该请求必须从我的项目应用程序中调用。因此,我构建了一个web服务来实现这一点,如果我直接在浏览器上调用web服务,它的工作状态很好。i、 例如,如果我通过如下所示的浏览器URL调用该服务,则该文件将在我系统的浏览器默认下载位置下载

例如:

网络服务代码:

@Path("/downloadservice")
public class DownloadFileWS {

    @POST
    @Path("/images")   
    @Produces({"image/jpeg,image/png"})
    public Response getImageFile() { 
        /String FILE_PATH = "/ngs/app/sample.png";

        File file = new File(FILE_PATH);    
        Logger.getLogger("!!!!!!!!!!!"+FILE_PATH);  
        System.out.println("@@@@@@@@"+FILE_PATH);
        ResponseBuilder response = Response.ok((Object) file);  

        response.header("Content-Disposition","attachment; filename=\"sample.png\""); 
        return response.build();  
    }  
public void getFileDownload(){  
        log("inside getServerPath....");
        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(downloadWebService_URL);
        JSONObject json = new JSONObject();
        json.put("filePath", "/ngs/app/sample.png");
        json.put("fileName", "sample.png");

        log("json-->"+json.toString());

        StringEntity inputJson = null;
        try {
            inputJson = new StringEntity(json.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } 
        log("inputJson = " + inputJson.toString());

        inputJson.setContentType("application/json");
        httpPost.setEntity(inputJson);
        httpPost.addHeader("AppType", "TC");

        log("httpPost... httpPost");
        HttpResponse response = null; 
        try {
            response = httpClient.execute(httpPost);
            log("response:-->"+response);
            log("response.getEntity()-->"+response.getEntity());
            log("response.getEntity().getContent()-->"+response.getEntity().getContent());


            response.setHeader("Content-Disposition", "attachment; filename=\"sample.png\"");

            InputStream is = response.getEntity().getContent();
            log("111");
            String filePath = "sample.txt";
            log("222"); 
            FileOutputStream fos = new FileOutputStream(new File(filePath));
            log("333"); 
            int inByte;
            while((inByte = is.read()) != -1){
                 fos.write(inByte);
                 log("444");
            }
            is.close();
            fos.close();

            log("completed...");
        }
        catch (ClientProtocolException e) {
            e.printStackTrace();
        }
        catch (IOException e) { 
            e.printStackTrace();
        }

        catch (Exception e) 
        {
            log("E:: " + ExceptionUtils.getStackTrace(e));
        }
    }
因此,在这里,我的浏览器充当客户端,它检索成功下载的文件

由于直接通过浏览器URL调用webservice存在安全问题,因此我需要通过我的应用程序java代码(未向最终用户公开)调用上述webservice。因此,在我的项目应用程序java代码中,我构建了HTTPClient并调用web服务,如下所示

客户端代码:

@Path("/downloadservice")
public class DownloadFileWS {

    @POST
    @Path("/images")   
    @Produces({"image/jpeg,image/png"})
    public Response getImageFile() { 
        /String FILE_PATH = "/ngs/app/sample.png";

        File file = new File(FILE_PATH);    
        Logger.getLogger("!!!!!!!!!!!"+FILE_PATH);  
        System.out.println("@@@@@@@@"+FILE_PATH);
        ResponseBuilder response = Response.ok((Object) file);  

        response.header("Content-Disposition","attachment; filename=\"sample.png\""); 
        return response.build();  
    }  
public void getFileDownload(){  
        log("inside getServerPath....");
        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(downloadWebService_URL);
        JSONObject json = new JSONObject();
        json.put("filePath", "/ngs/app/sample.png");
        json.put("fileName", "sample.png");

        log("json-->"+json.toString());

        StringEntity inputJson = null;
        try {
            inputJson = new StringEntity(json.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } 
        log("inputJson = " + inputJson.toString());

        inputJson.setContentType("application/json");
        httpPost.setEntity(inputJson);
        httpPost.addHeader("AppType", "TC");

        log("httpPost... httpPost");
        HttpResponse response = null; 
        try {
            response = httpClient.execute(httpPost);
            log("response:-->"+response);
            log("response.getEntity()-->"+response.getEntity());
            log("response.getEntity().getContent()-->"+response.getEntity().getContent());


            response.setHeader("Content-Disposition", "attachment; filename=\"sample.png\"");

            InputStream is = response.getEntity().getContent();
            log("111");
            String filePath = "sample.txt";
            log("222"); 
            FileOutputStream fos = new FileOutputStream(new File(filePath));
            log("333"); 
            int inByte;
            while((inByte = is.read()) != -1){
                 fos.write(inByte);
                 log("444");
            }
            is.close();
            fos.close();

            log("completed...");
        }
        catch (ClientProtocolException e) {
            e.printStackTrace();
        }
        catch (IOException e) { 
            e.printStackTrace();
        }

        catch (Exception e) 
        {
            log("E:: " + ExceptionUtils.getStackTrace(e));
        }
    }
当我单击应用程序中的图像链接时,我触发了上面提到的客户机java代码(getFileDownload()方法),该方法反过来使用DefaultHttpClient调用web服务

问题1: 客户端java代码正在调用web服务,我上面的web服务正在返回响应,如下所示

HTTP/1.1 200 OK[日期:2016年7月19日星期二21:47:42 GMT,内容长度: 6192,内容类型:图像/jpeg,内容配置:附件; filename=“sample.png”,X-Powered-By:Servlet/2.5 JSP/2.1] org.apache.http.conn。BasicManagedEntity@35ae84bf

WebService以文件下载的形式返回响应,但由于我的HTTPClient java代码收到响应,我不确定如何使用从我的WebService收到的此HTTPResponse,类似于在浏览器中下载文件。如果我单击应用程序中的链接,则不会下载文件,尽管请求和响应通信如上所述发生

问题2:

@Path("/downloadservice")
public class DownloadFileWS {

    @POST
    @Path("/images")   
    @Produces({"image/jpeg,image/png"})
    public Response getImageFile() { 
        /String FILE_PATH = "/ngs/app/sample.png";

        File file = new File(FILE_PATH);    
        Logger.getLogger("!!!!!!!!!!!"+FILE_PATH);  
        System.out.println("@@@@@@@@"+FILE_PATH);
        ResponseBuilder response = Response.ok((Object) file);  

        response.header("Content-Disposition","attachment; filename=\"sample.png\""); 
        return response.build();  
    }  
public void getFileDownload(){  
        log("inside getServerPath....");
        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(downloadWebService_URL);
        JSONObject json = new JSONObject();
        json.put("filePath", "/ngs/app/sample.png");
        json.put("fileName", "sample.png");

        log("json-->"+json.toString());

        StringEntity inputJson = null;
        try {
            inputJson = new StringEntity(json.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } 
        log("inputJson = " + inputJson.toString());

        inputJson.setContentType("application/json");
        httpPost.setEntity(inputJson);
        httpPost.addHeader("AppType", "TC");

        log("httpPost... httpPost");
        HttpResponse response = null; 
        try {
            response = httpClient.execute(httpPost);
            log("response:-->"+response);
            log("response.getEntity()-->"+response.getEntity());
            log("response.getEntity().getContent()-->"+response.getEntity().getContent());


            response.setHeader("Content-Disposition", "attachment; filename=\"sample.png\"");

            InputStream is = response.getEntity().getContent();
            log("111");
            String filePath = "sample.txt";
            log("222"); 
            FileOutputStream fos = new FileOutputStream(new File(filePath));
            log("333"); 
            int inByte;
            while((inByte = is.read()) != -1){
                 fos.write(inByte);
                 log("444");
            }
            is.close();
            fos.close();

            log("completed...");
        }
        catch (ClientProtocolException e) {
            e.printStackTrace();
        }
        catch (IOException e) { 
            e.printStackTrace();
        }

        catch (Exception e) 
        {
            log("E:: " + ExceptionUtils.getStackTrace(e));
        }
    }
在客户端java代码中,有没有任何方法可以在我们从WebService HTTPResponse对象获得响应后实现文件下载

问题3:

@Path("/downloadservice")
public class DownloadFileWS {

    @POST
    @Path("/images")   
    @Produces({"image/jpeg,image/png"})
    public Response getImageFile() { 
        /String FILE_PATH = "/ngs/app/sample.png";

        File file = new File(FILE_PATH);    
        Logger.getLogger("!!!!!!!!!!!"+FILE_PATH);  
        System.out.println("@@@@@@@@"+FILE_PATH);
        ResponseBuilder response = Response.ok((Object) file);  

        response.header("Content-Disposition","attachment; filename=\"sample.png\""); 
        return response.build();  
    }  
public void getFileDownload(){  
        log("inside getServerPath....");
        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(downloadWebService_URL);
        JSONObject json = new JSONObject();
        json.put("filePath", "/ngs/app/sample.png");
        json.put("fileName", "sample.png");

        log("json-->"+json.toString());

        StringEntity inputJson = null;
        try {
            inputJson = new StringEntity(json.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } 
        log("inputJson = " + inputJson.toString());

        inputJson.setContentType("application/json");
        httpPost.setEntity(inputJson);
        httpPost.addHeader("AppType", "TC");

        log("httpPost... httpPost");
        HttpResponse response = null; 
        try {
            response = httpClient.execute(httpPost);
            log("response:-->"+response);
            log("response.getEntity()-->"+response.getEntity());
            log("response.getEntity().getContent()-->"+response.getEntity().getContent());


            response.setHeader("Content-Disposition", "attachment; filename=\"sample.png\"");

            InputStream is = response.getEntity().getContent();
            log("111");
            String filePath = "sample.txt";
            log("222"); 
            FileOutputStream fos = new FileOutputStream(new File(filePath));
            log("333"); 
            int inByte;
            while((inByte = is.read()) != -1){
                 fos.write(inByte);
                 log("444");
            }
            is.close();
            fos.close();

            log("completed...");
        }
        catch (ClientProtocolException e) {
            e.printStackTrace();
        }
        catch (IOException e) { 
            e.printStackTrace();
        }

        catch (Exception e) 
        {
            log("E:: " + ExceptionUtils.getStackTrace(e));
        }
    }
我是否应该改变我的方法,如下链接所示


欢迎提出任何建议!提前谢谢

对这个问题有什么意见吗?对这个问题有什么意见吗?