[英]Getting HTTP 400 error when trying to download file using Jersey Client
如果我简单地将urlString
值粘贴到chrome浏览器中,就可以很好地下载csv文件。
但是,当我尝试在以下代码的帮助下使用相同的urlString
下载文件时,得到response.getStatus()
为400
错误
WebResource webResource = client.resource(urlString);
WebResource.Builder wb=webResource.accept("application/json,application/pdf,text/plain,image/jpeg,application/xml,application/vnd.ms-excel");
ClientResponse response =wb.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("HTTP error code : "
+ response.getStatus());
}
InputStream input = response.getEntity(InputStream.class);
byte[] byteArray = org.apache.commons.io.IOUtils.toByteArray(input);
FileOutputStream fos = new FileOutputStream(new File(fileToSave));
fos.write(byteArray);
fos.flush();
fos.close();
不过,在接受参数中,我不需要的只是text/plain
,只是为了扩大接受范围,我添加了更多内容。
花费大量时间尝试查找问题,请告知。 有很多类似的问题,但没有一个能解决我的问题。
我正在使用以下球衣版本
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.2</version>
您的代码原则上很好。
将其合并到JUnit测试中的位置。 (请参见下面的代码)。 如果现在将URL放入getCSVAsFile(url),则可以告诉您特定URL的情况。 可能是
WebResource.Builder wb=webResource.accept("application/json,application/pdf,text/plain,image/jpeg,application/xml,application/vnd.ms-excel");
行是元凶。 我先将其注释掉,因为它告诉Web服务您正在调用以向您发送特定的Representation / MediaType。 如果没有匹配项,则可能会导致错误。
基于JUnit测试
package com.bitplan.mediawiki.japi;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.client.apache.ApacheHttpClient;
/**
* test CSV access for test code
* @author wf
*
*/
public class TestGetCSV {
private ApacheHttpClient client;
/**
* get the a CSV File from the given urlString
* http://stackoverflow.com/questions/27224870/getting-http-400-error-when-trying-to-download-file-using-jersey-client
*
* @param urlString
* @param csvFile
* @throws IOException
*/
public void getCSVAsFile(String urlString, File csvFile) throws IOException {
client = ApacheHttpClient.create();
WebResource webResource = client.resource(urlString);
WebResource.Builder wb = webResource
.accept("application/json,application/pdf,text/plain,image/jpeg,application/xml,application/vnd.ms-excel");
ClientResponse response = wb.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("HTTP error code : " + response.getStatus());
}
InputStream input = response.getEntity(InputStream.class);
byte[] byteArray = org.apache.commons.io.IOUtils.toByteArray(input);
FileOutputStream fos = new FileOutputStream(csvFile);
fos.write(byteArray);
fos.flush();
fos.close();
}
@Test
public void testGetCSV() throws IOException {
boolean debug=false;
File csvFile=new File("/tmp/ExampleWikis.csv");
getCSVAsFile("http://mediawiki-japi.bitplan.com/mediawiki-japi/index.php/Special:Ask/-5B-5BCategory:ExampleWiki-5D-5D-20-5B-5Bsiteurl::%2B-5D-5D/-3FSiteurl/-3FWikiid/format%3Dcsv/offset%3D0", csvFile);
String csv=FileUtils.readFileToString(csvFile);
if (debug)
System.out.println(csv);
assertTrue(csv.contains("http://waihekepedia.org/"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.