简体   繁体   中英

java.io.IOException: Server returned HTTP response code: 500 for URL while uploading JSON object

This is the code on server side, please help me to understand where I am going wrong. I am uploading an byte array of image as JSON object. Converting the byte array and saving it on disk.

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();
    }
}

This is my client side code.

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();
        }
}
}

And the stack trace is java.io.IOException: Server returned HTTP response code: 500 for URL: http://:9999/FileUpload/api/crunchifyService at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439) at com.file.up.FileClient.main(FileClient.java:64)

我在TomEE服务器中复制了您的类,并且从客户端收到的请求中没有正文,您应该尝试使用jaxrs客户端

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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