80,429
社区成员
发帖
与我相关
我的任务
分享
URL url = new URL(
"http://192.168.191.104:8080/myapp/servlet/MyServlet");
HttpURLConnection connection = ((HttpURLConnection) url
.openConnection());
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.connect();
OutputStream out = connection.getOutputStream();
int len;
byte[] buffer = new byte[1024];
// 读取文件
FileInputStream fileInputStream = new FileInputStream(
Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ "/123.jpg");
while((len = fileInputStream.read(buffer, 0, 1024)) != -1){
out.write(buffer);
}
out.flush();
out.close();
fileInputStream.close();
InputStream input = connection.getInputStream();
while ((len = input.read(buffer)) != -1) {
Log.i("tag", "data:"
+ new String(buffer, 0, len));
}
input.close();
connection.disconnect();
ServletInputStream inputStream = ((ServletRequest) request)
.getInputStream();
int len, offset, count;
count = offset = 0;
byte[] buffer = new byte[1024];
try {
FileOutputStream outputStream = new FileOutputStream("D:\\123.jpg");
while ((len = inputStream.readLine(buffer, 0, 1024)) != -1) {
offset += len;
outputStream.write(buffer, 0, len);
}
outputStream.flush();
outputStream.close();
} catch (Exception e) {
System.out.println(e.toString());
}
PrintWriter out = response.getWriter();
out.println("ok");