gpt4 book ai didi

java - 使用 Jersey 通过 Java 的 POST API 调用发送文本/CSV 文件

转载 作者:行者123 更新时间:2023-12-01 17:29:20 26 4
gpt4 key购买 nike

我想通过发出 POST 请求来发送文件作为我的请求正文的一部分。文件的内容类型是text/csv。我正在创建一个 MultiPart,但没有选项可以发送文本/csv 文件。我尝试创建自定义内容类型 - text/csv 但它引发异常。下面是我的虚拟代码。

package com.practice;

import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;

public class CATestonlyForLocal1 {

public final static String TEXT_CSV = "text/csv";
public static final String CA_ENDPOINT_URL = "https://api.channeladvisor.com/v1/ProductUpload?";
public static final String CA_INVENTORY_TEMPLATE_CODE = "&templatecode=CAINV";
public static final String CA_PRICE_TEMPLATE_CODE = "CAPRICE";
public static final String CA_IMPORT_TYPE = "&importtype=AddUpdate";
public static final String CA_CONTENT_TYPE_VALUE = "text/csv";
public static final String CA_ACCESS_TOKEN = "wd52SrkWgoXeidEZ2nNIh_2gdveWIGWlKRYvUQQudYw-33727";
public static final String CA_FILE_FOLDER = "C:/D Drive/OMS Documents/Story Related Docs/Channel Advisor/InventoryUpload.txt";
public static final String CA_CONTENT_TYPE_KEY = "Content-Type";

public static void main(String[] args) {
try {
// getProductId();
uploadInventory();

} catch (Exception e) {
e.printStackTrace();
}
}

public static void uploadInventory() {

final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
File fileEntity = new File(CA_FILE_FOLDER);
final FileDataBodyPart filePart = new FileDataBodyPart("text/csv", fileEntity);
final MediaType TEXT_CSV_TYPE = new MediaType("text", "csv");
filePart.setMediaType(TEXT_CSV_TYPE);
FormDataMultiPart multipart = new FormDataMultiPart();
multipart.bodyPart(filePart);
WebTarget target = client.target(CA_ENDPOINT_URL + "access_token=" + CA_ACCESS_TOKEN + CA_INVENTORY_TEMPLATE_CODE + CA_IMPORT_TYPE);

System.out.println("multipart.getMediaType(): " + multipart.getMediaType());
System.out.println(TEXT_CSV);

final Response response = target.request().post(Entity.entity(multipart,multipart.getMediaType()));

System.out.println(response.readEntity(String.class));
}
}

下面是我使用自定义内容类型时遇到的异常

Apr 11, 2020 1:00:27 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=text/csv, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/csv, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:503)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:315)
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:227)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:246)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:664)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:664)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:424)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:333)
at com.practice.CATestonlyForLocal.uploadInventory(CATestonlyForLocal.java:58)
at com.practice.CATestonlyForLocal.main(CATestonlyForLocal.java:32)

我读了很多文档,但没有得到太多。

请帮忙。提前致谢。

最佳答案

您必须使用受支持的 MediaType。

尝试

javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE

关于java - 使用 Jersey 通过 Java 的 POST API 调用发送文本/CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61153491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com