小白又来提问题了,多方寻求无解
正在做上传使用的是HttpURLConnection加sessionID上传用户数据
昨天上传还很正常,下午快下班了就报错,老夫心痛 T_T
上传代码如下:
public static void upload(String host,String sessionID,File file,Map<String,String> params,FileUploadListener listener){
String BOUNDARY = UUID.randomUUID().toString(); //边界标识 随机生成 String PREFIX = "--" , LINE_END = "\r\n";
String CONTENT_TYPE = "multipart/form-data"; //内容类型
HttpURLConnection conn=null;
//System.setProperty("http.keepAlive", "false");
//System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
try {
URL url = new URL(host);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(TIME_OUT);
conn.setConnectTimeout(TIME_OUT);
conn.setRequestMethod("POST"); //请求方式
conn.setRequestProperty("cookie",sessionID);
conn.setRequestProperty("Charset", "utf-8");//设置编码
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY);
//conn.setDoInput(true); //允许输入流
conn.setDoOutput(true); //允许输出流
conn.setUseCaches(false); //不允许使用缓存
if(file!=null) {
/** * 当文件不为空,把文件包装并且上传 */
OutputStream outputSteam=conn.getOutputStream();
try {
DataOutputStream dos = new DataOutputStream(outputSteam);
conn.setChunkedStreamingMode(1024*100);
StringBuffer sb = new StringBuffer();
sb.append(LINE_END);
if(params!=null){//根据格式,开始拼接文本参数
for(Map.Entry<String,String> entry:params.entrySet()){
sb.append(PREFIX).append(BOUNDARY).append(LINE_END);//分界符
sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINE_END);
sb.append("Content-Type: text/plain; charset=" + CHARSET + LINE_END);
sb.append("Content-Transfer-Encoding: 8bit" + LINE_END);
sb.append(LINE_END);
sb.append(entry.getValue());
sb.append(LINE_END);//换行!
}
}
sb.append(PREFIX);//开始拼接文件参数
sb.append(BOUNDARY); sb.append(LINE_END);
sb.append("Content-Disposition: form-data; name=\"coverpic\"; filename=\""+file.getName()+"\""+LINE_END);
sb.append(LINE_END);
//写入文件数据
dos.write(sb.toString().getBytes());
InputStream is = new FileInputStream(file);
byte[] bytes = new byte[1024];
long totalbytes = file.length();
long curbytes = 0;
Log.i("cky", "total=" + totalbytes);
int len = 0;
while((len=is.read(bytes))!=-1){
curbytes += len;
dos.write(bytes, 0, len);
listener.onProgress(curbytes, 1.0d * curbytes / totalbytes);
}
is.close();
dos.write(LINE_END.getBytes());//一定还有换行
byte[] end_data = (PREFIX+BOUNDARY+PREFIX+LINE_END).getBytes();
dos.write(end_data);
dos.flush();
/**
* 获取响应码 200=成功
* 当响应成功,获取响应的流
*/
int code = conn.getResponseCode();
if (code == 200) {
InputStream iss = conn.getInputStream();
String state = getStringFromISS(iss);
listener.onFinish(state);
}
}catch (Exception e) {
Log.e("test", "捕获异常 " + e.getMessage() + " | " + e.getLocalizedMessage());
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (conn != null) conn.disconnect();
}
}</pre>
在debug的时候显示:
参考
http://stackoverflow.com/questions/23738940/httpurlconnection-java-lang-illegalstateexception-already-connected
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。