【问题标题】:How to upload to image hosting site in java?如何在java中上传到图像托管站点?
【发布时间】:2012-04-24 17:41:41
【问题描述】:

我正在尝试将图像上传到图像托管站点 (fastpic.ru),但我无法得到预期的正确响应。我用提琴手检查我是否会发送正确的参数,一切似乎都很好,但我无法得到正确的回应。您能指导我如何以正确的方式上传和获得响应吗?

正确的回应我的意思是我应该收到类似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();

【问题讨论】:

  • 好的,我在这里看到拇指向下。可能您认为我在询问之前没有进行研究,而是我在 1 天内尝试但没有结果的事实。所以至少给我应该读什么文章/帖子来学习。谢谢
  • "...我无法得到预期的正确响应..." 就目前而言,它毫无用处。
  • 您应该通过更准确地说明与您的期望不符以及您想要的结果来改进您的问题。你的问题太模糊,代码太长。
  • 好的,我在问题中解释了更多关于正确回答的内容并删除了一些行。你能再看一遍吗? @mario-de-schaepmeester
  • 如果您的问题已得到解答,或者不再有效,请“打勾”以选择最合适的答案,以便所有人都知道问题已得到解决。谢谢。

标签: java upload image-uploading


【解决方案1】:

如果它没有返回正确的输出,则可能是输入有问题。

我建议使用 Apache HTTP 组件,例如 MultipartEntity http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html 来发布此类数据。如果您尝试手动对数据进行编码,则很容易犯一个简单的错误,从而使整个事情停止工作。有很多使用 Apache 组件的示例,而且使用起来非常简单。

【讨论】:

  • 我用小提琴来比较我的应用程序和浏览器上传之间的输入数据,除了边界之外,两者都是相同的。问题是我不知道我的代码有什么问题,所以我希望有人能指出问题所在并指出我正确的方法。顺便说一句,我将查看 apache http 组件
猜你喜欢
  • 2017-04-24
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多