我是java的新手,过去几天我一直坚持这个问题。我正在将图像从java客户端发送到python服务器。我收到了200请求,意味着正在建立连接。但是图像没有在服务器端读取。我已经包含了代码 - 任何帮助都会很棒。
服务器代码
def index(request):
data = {"POST REQUEST, INSIDE INDEX, NO URL OR IMAGE SENT": False}
if request.method == "GET":
return HttpResponse("GET REQUEST WORKS. RR.")
if request.method == "POST":
# check to see if an image was uploaded
if request.FILES.get("image", None) is not None:
# grab the uploaded image
image = request.FILES["image"]
# otherwise, assume that a URL was passed in
else:
# grab the URL from the request
url = request.POST.get("url", None)
# if the URL is None, then return an error
if url is None:
return HttpResponse("no URL is specified")
# load the image and convert
image = _grab_image(url=url)
req = image
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr,-1) # 'load it as it is'
Server = True
Student_Answers = []
image = doc_Scan(img)
if (image == "ERROR"):
return HttpResponse("TAKE ANOTHER PICTURE")
else:
images = crop_answer2(image)
for i in range(0,8):
ans = images[i]
Answer = Input_Image(ans,Server)
print ('Final Answer = ', Answer)
Student_Answers.append(Answer)
Results, score = mark_paper(Server,Student_Answers)
Worded_Respose = "The Student got ", score, " correct"
return HttpResponse(Results, content_type="application/json")
else:
return HttpResponse("NO POST REQUEST")
客户代码
public static void main(String [] args) throws Exception{
String url = "http://########";
// 2. create obj for the URL class
URL obj = new URL(url);
// 3. open connection on the url
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
try {
System.out.println("Reading image from disk. ");
img = ImageIO.read(new File("image.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos);
baos.flush();
byte[] bytes = baos.toByteArray();
baos.close();
System.out.println("Sending image to server. ");
OutputStream out = con.getOutputStream();
DataOutputStream image = new DataOutputStream(out);
image.writeInt(bytes.length);
image.write(bytes, 0, bytes.length);
System.out.println("Image sent to server. ");
image.close();
// close the output stream
out.close();
}catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
soc.close();
}
// define object for the reply from the server
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
//Get response from server
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
// read in the response from the server
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
System.out.println(inputLine);
}
// close the input stream
in.close();
}
}