错误信息
java.io.IOException: Server returned HTTP response code: 411 for URL: http://view.wps.cn/service/dotview/process.php?act=upload
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1241)
at com.io.file.test.FileTest.main(FileTest.java:34)
Ojasdfjjava.io.IOException: Server returned HTTP response code: 411 for URL: http://view.wps.cn/service/dotview/process.php?act=upload
源码:
package com.io.file.test;import java.io.*;
import java.net.HttpURLConnection;
//import com.caucho.hessian.client.HessianProxyFactory;
import java.net.URL;public class FileTest { public static void main(String[] args) throws IOException {
try {
URL url = new URL("http://view.wps.cn/service/dotview/process.php?act=upload");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setChunkedStreamingMode(1024*1024);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", "UTF-8");
String fname = "D:/soft/面试题/java面试题01.doc";
File file = new File(fname);
// conn.setChunkedPost();
conn.setRequestProperty("Content-Type","multipart/form-data;file="+file.getName());
conn.setRequestProperty("filename",file.getName());
OutputStream out = new DataOutputStream(conn.getOutputStream());
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.out.println("Ojasdfj" + e);
e.printStackTrace();
}
}}
URLHttpConnection上传
java.io.IOException: Server returned HTTP response code: 411 for URL: http://view.wps.cn/service/dotview/process.php?act=upload
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1241)
at com.io.file.test.FileTest.main(FileTest.java:34)
Ojasdfjjava.io.IOException: Server returned HTTP response code: 411 for URL: http://view.wps.cn/service/dotview/process.php?act=upload
源码:
package com.io.file.test;import java.io.*;
import java.net.HttpURLConnection;
//import com.caucho.hessian.client.HessianProxyFactory;
import java.net.URL;public class FileTest { public static void main(String[] args) throws IOException {
try {
URL url = new URL("http://view.wps.cn/service/dotview/process.php?act=upload");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setChunkedStreamingMode(1024*1024);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", "UTF-8");
String fname = "D:/soft/面试题/java面试题01.doc";
File file = new File(fname);
// conn.setChunkedPost();
conn.setRequestProperty("Content-Type","multipart/form-data;file="+file.getName());
conn.setRequestProperty("filename",file.getName());
OutputStream out = new DataOutputStream(conn.getOutputStream());
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.out.println("Ojasdfj" + e);
e.printStackTrace();
}
}}
URLHttpConnection上传
1、可能是没有传http中的content-length参数,这个一般都有。
2、可能是因为请求方式错误的,本来只是获取数据,应该使用GET的方式;但是你使用的是POST的方式。