gpt4 book ai didi

java - 将文件(图像)从 Android 设备发送到使用 REST 编写的服务器上的 Web 服务

转载 作者:太空狗 更新时间:2023-10-29 16:35:50 26 4
gpt4 key购买 nike

<分区>

我想将图像从 Android 设备发送到我的 Web 应用程序,该应用程序运行在带有 Tomcat 的服务器上。请帮助我编写用于将图像发送到在 Web 服务器上运行的 REST Web 服务的小代码。如果可能,请提供示例代码。我坚持使用什么方法。任何帮助将不胜感激。提前致谢。

编辑:这个问题的答案如下

while(it.hasNext()){
File file = new File((new StringBuilder()).append(Environment.getExternalStorageDirectory()).append(File.separator).append("jcms").append(File.separator).append("Customer_").append( customer.getId()).toString());
File[] listOfFiles = file.listFiles();

for(int i=0;i<listOfFiles.length;i++){
JSONObject message = new JSONObject();
File fil=listOfFiles[i];
FileInputStream imageInFile = new FileInputStream(fil);
byte imageData[] = new byte[(int)fil.length()];
imageInFile.read(imageData);
String imageDataString = encodeImage(imageData);



URL url=new URL(ClearCustomersContract.CLEAR_SERVER_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestMethod("POST");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(imageDataString);
out.close();

BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
while (in.readLine() != null) {
}
in.close();
}
}

服务器端的 REST Webservice 就像

@Override
@POST
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_OCTET_STREAM})
@Path("/getData")
public Response getAllTheSyncData(InputStream incomingData) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
System.out.println("Error Parsing: - ");
}
return Response.status(200).entity("Success").build();
}

这就是我们将字符串转换回图像的方式。

byte[] imageByteArray = decodeImage(jsonObj.get("imageData").toString());

               imageOutFile = new FileOutputStream(
"C:/Users/SUNILKUMAR/Desktop/result.jpg");

// Write a image byte array into file system

imageOutFile.write(imageByteArray);


imageOutFile.close();

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com