80,351
社区成员
发帖
与我相关
我的任务
分享
private void uploadFile() {
try {
String end = "\r\n";
String twoHyphens = "--";
// 分割符号
String boundary = "*****";
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setReadTimeout(5000);
/* 允许Input、Output,不使用Cache */
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
/* 设置传送的method=POST */
httpURLConnection.setRequestMethod("POST");
/* setRequestProperty */
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
httpURLConnection.setChunkedStreamingMode(1024);
httpURLConnection.connect();
/* 设置DataOutputStream */
DataOutputStream dataOutputStream = new DataOutputStream(
httpURLConnection.getOutputStream());
dataOutputStream.writeBytes(twoHyphens + boundary + end);
dataOutputStream.writeBytes("Content-Disposition: form-data; "
+ "name =\"file1\";filename=\"" + newName + "\"" + end);
dataOutputStream.writeBytes(end);
/* 取得文件的FileInputStream */
File file = new File(uploadFile);
fileLenth = file.length();
Log.d(LOG_STRING, "文件的总长度为" + file.length());
FileInputStream fileInputStream = new FileInputStream(file);
/* 设置每次写入1024bytes */
int bufSize = 1024;
byte[] buf = new byte[bufSize];
int length = -1;
/* 从文件读取数据至缓冲区 */
int num = 0;
double speed = 0;
while ((length = fileInputStream.read(buf)) != -1) {
/* 将资料写入DataOutputStream中 */
// SystemClock.sleep(100);
dataOutputStream.write(buf, 0, length);
dataOutputStream.flush();
num = num + length;
speed = (double) num / (double) fileLenth;
DecimalFormat df = new DecimalFormat("#.00");
int en = (int) (Double.valueOf(df.format(speed)) * 100);
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putInt("data", en);
message.what = 1;
message.setData(bundle);
handler.sendMessage(message);
Log.e(LOG_STRING, "正在写入" + num);
}
dataOutputStream.writeBytes(end);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ end);
/* close streams */
fileInputStream.close();
dataOutputStream.flush();
/* 取得Response内容 */
Log.v(LOG_STRING, "正在发送数据");
InputStream is = httpURLConnection.getInputStream();
Log.v(LOG_STRING, "已经发送完数据");
int ch;
StringBuffer buffer = new StringBuffer();
while ((ch = is.read()) != -1) {
buffer.append((char) ch);
Log.d(LOG_STRING, "读取服务器信息" + buffer.toString());
}
if (buffer.toString().equals("success")) {
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putInt("data", 1000);
message.what = 1;
message.setData(bundle);
handler.sendMessage(message);
Log.d(LOG_STRING, "上传成功");
}
/* 将Response显示于Dialog */
/* 关闭DataOutputStream */
dataOutputStream.close();
httpURLConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putInt("data", 1000);
message.what = 1;
message.setData(bundle);
handler.sendMessage(message);
}
}