简体   繁体   English

尝试使用Jersey客户端下载文件时出现HTTP 400错误

[英]Getting HTTP 400 error when trying to download file using Jersey Client

I am able to download the csv file fine if I simpley paste the urlString value in chrome browser. 如果我简单地将urlString值粘贴到chrome浏览器中,就可以很好地下载csv文件。

But, when I am trying to download the file using same urlString with help of below code I get response.getStatus() as 400 error 但是,当我尝试在以下代码的帮助下使用相同的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();

Though, I don't need more than text/plain in my accept param, just to widen the accept, I added more. 不过,在接受参数中,我不需要的只是text/plain ,只是为了扩大接受范围,我添加了更多内容。

Spent lot of time trying to find the issue, please advise. 花费大量时间尝试查找问题,请告知。 There are many similar questions, but none solves my problem. 有很多类似的问题,但没有一个能解决我的问题。

I am using following jersey version 我正在使用以下球衣版本

    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.18.2</version>

Your code is fine in principle. 您的代码原则上很好。

See https://github.com/WolfgangFahl/Mediawiki-Japi/blob/master/src/test/java/com/bitplan/mediawiki/japi/TestGetCSV.java#L49 参见https://github.com/WolfgangFahl/Mediawiki-Japi/blob/master/src/test/java/com/bitplan/mediawiki/japi/TestGetCSV.java#L49

where it is incorporated in a JUnit test. 将其合并到JUnit测试中的位置。 (See code below). (请参见下面的代码)。 If you now put your url into getCSVAsFile(url) it would be possible to tell you what's going on whith a specific url. 如果现在将URL放入getCSVAsFile(url),则可以告诉您特定URL的情况。 Probably the 可能是

WebResource.Builder wb=webResource.accept("application/json,application/pdf,text/plain,image/jpeg,application/xml,application/vnd.ms-excel");

line is the culprit. 行是元凶。 I'd comment it out for a start because it tells the webservice you are calling to send you a specific Representation/MediaType. 我先将其注释掉,因为它告诉Web服务您正在调用以向您发送特定的Representation / MediaType。 If there is no match you might end up with an error. 如果没有匹配项,则可能会导致错误。

JUnit-Test 基于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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM