【问题标题】:java.io.IOException: Server returned HTTP response code: 500 for URL while uploading JSON objectjava.io.IOException:服务器返回 HTTP 响应代码:上传 JSON 对象时 URL 为 500
【发布时间】:2014-12-17 08:07:46
【问题描述】:

这是服务器端的代码,请帮助我理解我哪里出错了。 我正在将图像的字节数组作为 JSON 对象上传。转换字节数组并将其保存在磁盘上。

package com.file.up;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.imageio.ImageIO;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.json.JSONObject;

@Path("/")

public class FileUp {

    @POST
    @Path("/crunchifyService")
    @Consumes(MediaType.APPLICATION_JSON)

    public Response crunchifyREST(JSONObject incomingData) {
        String s="Success!";
        try {

            String jsonString = incomingData.getString("image");
            byte[] a=jsonString.getBytes();

            InputStream input=new ByteArrayInputStream(a);
            BufferedImage b=ImageIO.read(input);
            ImageIO .write(b,"png",new File("C:\\Users\\Uma\\Desktop\\WEB_AND"));

            } catch (Exception e) {
            System.out.println("Error Parsing: - ");
        }
        System.out.println("Data Received: ");

        // return HTTP response 200 in case of success
        return Response.status(200).entity(s).build();
    }
}

这是我的客户端代码。

package com.file.up;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection; 
import java.util.Base64;

import javax.imageio.ImageIO;
import org.json.JSONObject;

public class FileClient {

public static void main(String[] args) throws IOException {
    BufferedImage image;
    image = ImageIO.read(new File("12.png"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write( image, "png", baos );
    baos.flush();
    byte[] imageInByte = baos.toByteArray();
    String base64String = Base64.getEncoder().encodeToString(imageInByte);
    baos.close();

        // Step2: Now pass JSON File Data to REST Service
        try {
            JSONObject jsonObject = new JSONObject("{\"image\":\"" + base64String + "\"}");
            System.out.println(jsonObject);
            URL url = new URL("http://<ip>:9999/FileUpload/api/crunchifyService");
            URLConnection connection = url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
           // out.write(jsonObject.toString());
            out.close();



           BufferedReader in = new BufferedReader(new InputStreamReader(
                   connection.getInputStream()));

           while (in.readLine() != null) {
            }
            System.out.println("\nREST Service Invoked Successfully..");
            in.close();
        } catch (Exception e) {
            System.out.println("\nError while calling REST Service");
            System.out.println(e);
            e.printStackTrace();
        }
}
}

堆栈跟踪是 java.io.IOException:服务器返回 HTTP 响应代码:500 用于 URL:http://:9999/FileUpload/api/crunchifyService 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439) 在 com.file.up.FileClient.main(FileClient.java:64)

【问题讨论】:

  • 你能看到服务器日志吗?它可能会显示导致异常的原因。
  • 抱歉回复晚了。这是我的服务器日志:com.sun.jersey.api.core.ClasspathResourceConfig init INFO:扫描路径中的根资源和提供程序类:C:\jeresy webapp\apache-tomcat-7.0.57\webapps\FileUpload\WEB -INF\lib C:\jeresy webapp\apache-tomcat-7.0.57\webapps\FileUpload\WEB-INF\classes 2014 年 12 月 22 日上午 10:08:50 com.sun.jersey.api.core.ClasspathResourceConfig init INFO :找到根资源类:com.file.up.FileUp 类 2014 年 12 月 22 日上午 10:08:50 com.sun.jersey.api.core.ClasspathResourceConfig 初始化信息:找到提供程序类:
  • 这些只是 INFO 日志。是否有任何异常被抛出?我想 500 的原因会被记录下来。
  • 没有抛出异常
  • 如果我将服务器端更改为: public Response crunchifyREST(InputStream incomingData) 如果我将其设为 InputStream 则代替 JSONObject。服务器端至少被执行(尽管有例外)。我该怎么办?

标签: java json jersey jax-rs


【解决方案1】:

我在我的 TomEE 服务器中复制了您的课程,但从客户端我没有收到任何有关请求的正文,您应该尝试使用 jaxrs 客户端

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    相关资源
    最近更新 更多