public class FileUploadTask extends AsyncTask<Object, Integer, Void> {
private ProgressDialog dialog = null;
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
//图片地址
String uploadedFile = path;
//上传图片网址
String url = "http://192.168.1.38/camera10/photosumbit.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
File uploadFile = new File(uploadedFile);
long totalSize = uploadFile.length(); // 获取文件的大小
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(EditActivity.this);
dialog.setMessage("正在上传...");
dialog.setIndeterminate(false);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
dialog.show();
}
@Override
protected Void doInBackground(Object... arg0) {
long length = 0;
int progress;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 256 * 1024;// 256KB
try {
FileInputStream fileInputStream = new FileInputStream(new File(
uploadedFile));
URL urlstr = new URL(url);
connection = (HttpURLConnection) urlstr.openConnection();
connection.setChunkedStreamingMode(128 * 1024);// 128KB //上传速度限制
// 可以读取
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
//用post方法上传
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
//int userId=UserHelper.userId;
outputStream = new DataOutputStream(connection.getOutputStream());
//outputStream.writeInt(userId);
outputStream.writeBytes(twoHyphens + boundary + lineEnd); //上传文件格式
outputStream
.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
+ uploadedFile + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// 读取文件
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
//ds.write(value.getBytes("UTF-8"));
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
length += bufferSize;
progress = (int) ((length * 100) / totalSize);
publishProgress(progress);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
publishProgress(100);
// 获取服务器信息
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
//写入int数据
//outputStream.writeInt(userId);
outputStream.flush();
outputStream.close();
} catch (Exception ex) {
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
dialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(Void result) {
try {
dialog.dismiss();
// TODO Auto-generated method stub
} catch (Exception e) {
}
}
} 我想在这个类里上传图片的同时,上传一个int变量,然后在服务器那边通过post获取这个int的值
private ProgressDialog dialog = null;
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
//图片地址
String uploadedFile = path;
//上传图片网址
String url = "http://192.168.1.38/camera10/photosumbit.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
File uploadFile = new File(uploadedFile);
long totalSize = uploadFile.length(); // 获取文件的大小
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(EditActivity.this);
dialog.setMessage("正在上传...");
dialog.setIndeterminate(false);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
dialog.show();
}
@Override
protected Void doInBackground(Object... arg0) {
long length = 0;
int progress;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 256 * 1024;// 256KB
try {
FileInputStream fileInputStream = new FileInputStream(new File(
uploadedFile));
URL urlstr = new URL(url);
connection = (HttpURLConnection) urlstr.openConnection();
connection.setChunkedStreamingMode(128 * 1024);// 128KB //上传速度限制
// 可以读取
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
//用post方法上传
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
//int userId=UserHelper.userId;
outputStream = new DataOutputStream(connection.getOutputStream());
//outputStream.writeInt(userId);
outputStream.writeBytes(twoHyphens + boundary + lineEnd); //上传文件格式
outputStream
.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
+ uploadedFile + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// 读取文件
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
//ds.write(value.getBytes("UTF-8"));
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
length += bufferSize;
progress = (int) ((length * 100) / totalSize);
publishProgress(progress);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
publishProgress(100);
// 获取服务器信息
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
//写入int数据
//outputStream.writeInt(userId);
outputStream.flush();
outputStream.close();
} catch (Exception ex) {
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
dialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(Void result) {
try {
dialog.dismiss();
// TODO Auto-generated method stub
} catch (Exception e) {
}
}
} 我想在这个类里上传图片的同时,上传一个int变量,然后在服务器那边通过post获取这个int的值
connection.setRequestProperty("变量名", 数值);