基于CXF restwebservice

本文介绍了一个使用Apache HttpClient从RESTful Web服务下载文件到手机端的应用案例。通过客户端和服务端代码示例,展示了如何设置HTTP请求以获取并保存远程文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

应用场景:

手机端使用apache的httpclient组件。

服务器端使用spring+cxf rest webservice

手机端代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;


public class HttpClientTest {  
    
    private final static String REMOTE_FILE_URL = "http://localhost:8080/cxf_restful_webservice/rws/pdf/get";  
      
    private final static int BUFFER = 1024;  
 
    public static void main(String[] args) {  
 
       HttpClient client = new HttpClient();  
       PostMethod httpGet = new PostMethod(REMOTE_FILE_URL);  
        try {  
            client.executeMethod(httpGet);  
              
            InputStream in = httpGet.getResponseBodyAsStream();  
            Header[] h =  httpGet.getResponseHeaders("Content-Disposition");
//            NameValuePair ss = httpGet.getParameter("Content-Disposition");
//            String name = ss.getName();
//            String value = ss.getValue();
            String dispositionValue = h[0].getValue();
            int index = dispositionValue.indexOf("=");
            String fileName = dispositionValue.substring(index+1);
            System.out.println(h[0]);
            System.out.println(fileName);
            FileOutputStream out = new FileOutputStream(new File("D:\\upload\\"+fileName));  
             
            byte[] b = new byte[BUFFER];  
            int len = 0;  
            while((len=in.read(b))!= -1){  
                out.write(b,0,len);  
            }  
            in.close();  
            out.close();  
              
        }catch (HttpException e){  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }finally{  
            httpGet.releaseConnection();  
        }  
        System.out.println("download, success!!");  
       }  
}  

服务端代码:

import java.io.File;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

@Path("/zip")
public class PdfService {

    private static final String FILE_PATH = "c:\\xxx.zip";

    @POST
    @Path("/get")
    @Produces("application/pdf")
    public Response getFile() {

        File file = new File(FILE_PATH);

        ResponseBuilder response = Response.ok((Object) file);
        response.header("Content-Disposition",
                "attachment; filename=xxx.zip");
        return response.build();

    }

}

             

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值