我在本地弄个iis,ip为“192.168.5.200”,,
使用URL url = new URL("http", ip, "");来连接网络。
其余的上传操作都是采用网络上现有的上传代码,但为什么最终都是上传失败,返回值为405。
已排除本地iis的问题,我采用socket编程已成功上传。部分代码如下: String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
//URL url =new URL(actionUrl);
URL url =new URL("http", "192.168.5.200","");
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设定传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary="+boundary);
/* 设定DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=\"file1\";filename=\"" +
newName +"\"" + end);
ds.writeBytes(end);
ds.flush(); /* 取得文件的FileInputStream */
File file = new File(uploadFile);
long size = file.length();
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设定每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize]; int length = -1;
/* 从文件读取数据到缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将数据写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end); /* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
int res = con.getResponseCode();
InputStream is = con.getInputStream();
}却最终得到的结果都是con.getResponseCode() = 405,,上传失败后。调用
InputStream is = con.getInputStream();得到异常。
我有个疑惑的地方是,http上传协议中,post上传文件要有
Content-Length: 这个字段,这个是文件的大小,但是网络上的android上传却都没有这个参数设置。
使用URL url = new URL("http", ip, "");来连接网络。
其余的上传操作都是采用网络上现有的上传代码,但为什么最终都是上传失败,返回值为405。
已排除本地iis的问题,我采用socket编程已成功上传。部分代码如下: String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
//URL url =new URL(actionUrl);
URL url =new URL("http", "192.168.5.200","");
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设定传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary="+boundary);
/* 设定DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=\"file1\";filename=\"" +
newName +"\"" + end);
ds.writeBytes(end);
ds.flush(); /* 取得文件的FileInputStream */
File file = new File(uploadFile);
long size = file.length();
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设定每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize]; int length = -1;
/* 从文件读取数据到缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将数据写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end); /* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
int res = con.getResponseCode();
InputStream is = con.getInputStream();
}却最终得到的结果都是con.getResponseCode() = 405,,上传失败后。调用
InputStream is = con.getInputStream();得到异常。
我有个疑惑的地方是,http上传协议中,post上传文件要有
Content-Length: 这个字段,这个是文件的大小,但是网络上的android上传却都没有这个参数设置。
Accept:*/*
Host:192.168.0.164
Connection: Keep-Alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 9747后面跟着文件的内容。我试验过,在本地的iis服务器采用socket编程put方式上传成功。post失败,。
是不是因为没有脚本来处理客户端的上传请求。