java - 如何在 CXF JAX-RS 中将文件作为附件发布

标签 java http cxf jax-rs

我正在尝试发布一个使用 cxf 和 jaxrs 使用 gzip 压缩的 csv.file。

下面是服务器端代码。

import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.zip.GZIPInputStream;

import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;

import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.ext.multipart.Multipart;

import org.kp.common.LogConstants;
 import org.kp.util.LogHelper;

@Path("TestData")
 public class TestDataResource {

  @POST
  @Produces("text/xml")
  @Consumes(MediaType.APPLICATION_OCTET_STREAM)
  public Response postTestData( final @Multipart InputStream stream,
    @Context HttpServletRequest request) {

    boolean result = true;

    LogHelper.setLog(LogConstants.INFO, request.getContentType());
    LogHelper.setLog(LogConstants.INFO, request.getCharacterEncoding());
    LogHelper.setLog(LogConstants.INFO, request.getHeader("charset"));
    LogHelper.setLog(LogConstants.INFO, request.getHeader("Content-Encoding"));
    LogHelper.setLog(LogConstants.INFO, request.getHeader("Content-Length"));
    LogHelper.setLog(LogConstants.INFO, request.getHeader("Transfer-Encoding"));


    writeToFile(stream);
    return Response.status(result == true ? Status.OK : Status.EXPECTATION_FAILED).build();

  }

 private void writeToFile(InputStream inputStream) {

  OutputStream outputStream = null;

  try {

   GZIPInputStream gzis = new GZIPInputStream(inputStream);
    // write the inputStream to a FileOutputStream
    outputStream = new FileOutputStream(new File(
      "d:\\temp\\test.csv"));
    IOUtils.copy(gzis, outputStream);
   } catch (IOException ex) {
    ex.printStackTrace();
   }
   finally{
    try {
     outputStream.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

}

客户端代码如下。

import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.util.LinkedList;
 import java.util.List;

import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;


 public class kpTest {

 public static void main(String[] str) throws FileNotFoundException{

  final String  URL = "http://localhost:8080/kp_services/services/kpservices/TestData";

  WebClient client = WebClient.create(URL);

  //client.type(MediaType.APPLICATION_OCTET_STREAM);
   client.type(MediaType.APPLICATION_OCTET_STREAM);
   ContentDisposition cd = new ContentDisposition("attachment;filename=test.csv.gz");
   List<Attachment> atts = new LinkedList<Attachment>();
   InputStream stream = new FileInputStream("D:\\kp\\test.csv.gz");
   Attachment att = new Attachment("root", stream, cd);
   atts.add(att);
   //Response response = client.post(new MultipartBody(att));

  // or just post the attachment if it's a single part request only
  // Response response = client.post(atts);

  // or just use a file
   //client.post(new File("D:\\kp\\test.csv"));

   Response response = client.post(new File("D:\\kp\\test.csv.gz"));
   System.out.println(response.getStatus());
  }
 }

上面的程序运行良好。

Response response = client.post(new File("D:\\kp\\test.csv.gz"));

根据上面的代码,我认为我没有将文件作为附件发送,而是将其作为输入流发送,并使用了 url 编码。

当我尝试通过注释上面的行并取消注释来修改代码时

Response response = client.post(atts);

我收到错误消息说找不到消息正文作者。我什至尝试将服务器端代码从 @Multipart InputStream stream 更改为 List attachement。我仍然遇到同样的错误。我需要如何为消息正文编写器添加提供程序。有人可以帮我解决这个问题吗。

最佳答案

通过如下替换 MediaType.APPLICATION_OCTET_STREAM 和 InputStream 流来修复它

@Consumes("multipart/form-data")
  public Response postTestData( final List<Attachment> attachments,
    @Context HttpServletRequest request)

关于java - 如何在 CXF JAX-RS 中将文件作为附件发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18144695/

相关文章:

java - 如何在 Android 上创建可滚动的弹出窗口?

java - 在方法内传递对象类型变量并将其转换为 if 语句内的特定类

php - 通过代理后应跳过哪些 HTTP header ?

调用 WSDL 服务时出现 javax.xml.ws.WebServiceException

java - Eclipse Debug模式查看实例变量值

json - cURL GET 查询字符串是一个 JSON

java - HTTPUrlConnection 和 .torrent 文件

spring - JAX-WS 的注释端点位置

jax-ws - cxf 和 jax-ws 有何关系

java - 使用 $$context 签署 JNLP 在 java 7.45 中不起作用