2

我正在尝试使用 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列表附件更改。我仍然遇到同样的错误。如何我需要为消息正文作者添加提供者。一些人可以帮助我解决这个问题。

4

1 回答 1

1

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

@Consumes("multipart/form-data")
  public Response postTestData( final List<Attachment> attachments,
    @Context HttpServletRequest request)
于 2013-09-25T20:46:27.753 回答