代码如下:
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();
9 个解决方案
#1
服务器的代码如下:
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");
#2
setRequestProperty主要是设置HttpURLConnection请求头里面的属性
比如Cookie、User-Agent(浏览器类型)等等,具体可以看HTTP头相关的材料
至于要设置什么这个要看服务器端的约定
比如Cookie、User-Agent(浏览器类型)等等,具体可以看HTTP头相关的材料
至于要设置什么这个要看服务器端的约定
#3
设置头信息的,比如格式,UA等,不设置自然有默认的,一般的请求倒不需要去设置,可以去看看android里的DefaultHttpClient里也有设置头信息的
#4
#5
恩,3楼正解吧~
#6
请问楼主,如何设置HTTP头 版本为HTTP2.0 ??? 急!qq:348224249
#7
可不可以把代码贴全啊,本人Android菜鸟,希望能多学习
#8
HTTP sends such requests with a Content-Type: multipart/form-data header or a
Content-Type: multipart/mixed header and a multipart body, like this:
Content-Type: multipart/form-data; boundary=[abcdefghijklmnopqrstuvwxyz]
where the boundary specifies the delimiter string between the different parts of the
body.
Content-Type: multipart/mixed header and a multipart body, like this:
Content-Type: multipart/form-data; boundary=[abcdefghijklmnopqrstuvwxyz]
where the boundary specifies the delimiter string between the different parts of the
body.
#9
学习一下。。。。。
#1
服务器的代码如下:
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");
#2
setRequestProperty主要是设置HttpURLConnection请求头里面的属性
比如Cookie、User-Agent(浏览器类型)等等,具体可以看HTTP头相关的材料
至于要设置什么这个要看服务器端的约定
比如Cookie、User-Agent(浏览器类型)等等,具体可以看HTTP头相关的材料
至于要设置什么这个要看服务器端的约定
#3
设置头信息的,比如格式,UA等,不设置自然有默认的,一般的请求倒不需要去设置,可以去看看android里的DefaultHttpClient里也有设置头信息的
#4
setRequestProperty 主要是用来设置下面的功能
或者
有时候你需要给 connection 指定 Content-type。
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
或者
Connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
有时候你需要给 connection 指定 Content-type。
#5
恩,3楼正解吧~
#6
请问楼主,如何设置HTTP头 版本为HTTP2.0 ??? 急!qq:348224249
#7
可不可以把代码贴全啊,本人Android菜鸟,希望能多学习
#8
HTTP sends such requests with a Content-Type: multipart/form-data header or a
Content-Type: multipart/mixed header and a multipart body, like this:
Content-Type: multipart/form-data; boundary=[abcdefghijklmnopqrstuvwxyz]
where the boundary specifies the delimiter string between the different parts of the
body.
Content-Type: multipart/mixed header and a multipart body, like this:
Content-Type: multipart/form-data; boundary=[abcdefghijklmnopqrstuvwxyz]
where the boundary specifies the delimiter string between the different parts of the
body.
#9
学习一下。。。。。