Android http post 文件 bad request 400
来源:互联网 发布:计划进度表软件 编辑:程序博客网 时间:2024/06/05 06:53
一直出现bad request的错误,缺少httpclient-4.2.1.jar
public class UploadUtil {
private static final int TIME_OUT = 10 * 1000; // 超时时间
private static final String CHARSET = "utf-8"; // 设置编码
public void uploadFile(File file, String RequestURL) {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
URL url =new URL(RequestURL);
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=\"file\";filename=\"" +
file.getName() +"\"" + end);
ds.writeBytes(end);
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(file);
/* 设置每次写入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内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
Log.v("out","Upload Success"+b.toString().trim());
/* 关闭DataOutputStream */
ds.close();
}
catch(Exception e)
{
Log.v("out", "Upload Fail");
}
}
}
- Android http post 文件 bad request 400
- 400 (bad request) post
- http 400 Bad Request
- HTTP/1.1 400 Bad Request
- HTTP 400 Bad-Request解决方法
- HTTP 错误 400 - Bad Request
- 分享失败 http/1.1 400 bad request 【sina android sdk】
- WCF Rest post 400 bad request
- Ajax post 报400 bad request错误
- Http的Get/Post请求带有特殊字符,400 Bad Request解决方案
- Http的Get/Post请求带有特殊字符,400 Bad Request解决方案
- Http的Get/Post请求带有特殊字符,400 Bad Request解决方案
- 研发过程遇到HTTP 400 Bad Request
- HTTP Status 400 – Bad Request
- HTTP 1.1 400 Bad Request 11ms
- BUG:HTTP 400错误(Bad request)
- Error parsing HTTP request header--400 bad request
- django 上传文件出现bad request(400)
- 如何在Linux上配置基于Web的网络流量监控系统
- iOS 7 NavigationBar 完全透明
- 有关编译
- struts2
- 笔试面试必会代码 以及必看书籍
- Android http post 文件 bad request 400
- 查询低效SQL的语句
- OC学习心得(一)
- 使用NSURLProtocol实现UIWebView的离线缓存
- Date和String转换
- 介绍6款存储系统和磁盘压力测试工具
- 多线程内存模型
- Xcode5.0 使用github
- linux I2C驱动分析整理