2012-04-24 85 views
-3

我试图上传图像到图像托管网站(fastpic.ru),但我无法得到正确的响应,如我所料。我用提琴手来检查我会发送正确的参数,一切似乎都很好,但我无法得到正确的答案。你能指导我如何上传并以适当的方式得到回应吗?如何上传到图像托管网站在Java?

正确的反应我的意思是我应该得到的东西像http://fastpic.ru/session/2012/0425/Y6sEtGjtT1.html但我只收到http://fastpic.ru/index.php

谢谢

这是我的代码

String urlToConnect = "http://fastpic.ru/uploadmulti"; 
    String boundary = Long.toHexString(System.currentTimeMillis()); // Generate random boundary 
    URLConnection connection = new URL(urlToConnect).openConnection(); 
    connection.setDoOutput(true); // This sets request method to POST. 
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 
    OutputStream output = null; 
    PrintWriter writer = null; 
    try { 
     output = connection.getOutputStream(); 
     writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true); // true = Autoflush, important! 

     writer.println("-----------------------------" + boundary); 
     writer.println("Content-Disposition: form-data; name=\"file[]\"; filename=\"" + fileToUpload.getName() + "\""); 
     writer.println("Content-Type: image/jpeg"); 
     writer.println(); 
     InputStream input = null; 
     try { 
      input = new FileInputStream(fileToUpload); 
      byte[] buffer = new byte[1024]; 
      for (int length = 0; (length = input.read(buffer)) > 0;) { 
       output.write(buffer, 0, length); 
      } 
      output.flush(); 
     } finally { 
      if (input != null) { 
       try { 
        input.close(); 
       } catch (IOException logOrIgnore) { 
       } 
      } 
     } 
     writer.println(); 

     writer.println("-----------------------------" + boundary); 
     writer.println("Content-Disposition: form-data; name=\"submit\""); 
     writer.println(); 
     writer.println("Загрузить"); 

     writer.println("-----------------------------" + boundary); 
     writer.println("Content-Disposition: form-data; name=\"uploading\""); 
     writer.println(); 
     writer.println("1"); 
     writer.println("-----------------------------" + boundary + "--"); 

    } finally { 
     if (writer != null) { 
      writer.close(); 
     } 
    } 

    BufferedReader in = new BufferedReader(
           new InputStreamReader(
           connection.getInputStream())); 
    String decodedString; 
    while ((decodedString = in.readLine()) != null) { 
     System.out.println(decodedString); 
    } 
    in.close(); 
+0

好的我在这里看到拇指。可能你认为我之前没有研究过,但是我在1天内没有结果尝试过。所以至少给我什么文章/职位我应该读取学习。谢谢 – KingOfDCP 2012-04-24 18:18:57

+1

*“......我无法得到正确的答复,因为我预期......”*是非常没用的,因为它代表。 – 2012-04-24 18:23:29

+2

您应该更准确地说明根据您的期望不能达到的目标以及您想要的结果,从而改善您的问题。你的问题太含糊,代码太长。 – MarioDS 2012-04-24 18:23:46

回答

1

如果没有返回正确的输出,输入可能有不正确的地方。

我会推荐使用Apache HTTP组件,如MultipartEntityhttp://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html用于发布此类数据。如果您试图手动对数据进行编码,那么很容易犯一个简单的错误来阻止整个事情的发生。有很多使用Apache组件的例子,它的使用非常简单。

+0

我用小提琴来比较我的应用程序和浏览器上传之间的输入数据,除边界外两者都是相同的。问题是我不知道我的代码中出了什么问题,所以我希望有些人可以指出什么是错的,并指向正确的方向。顺便说一句,我会看看Apache的HTTP组件 – KingOfDCP 2012-04-25 11:16:04

相关问题