想在后台使用okhttp 3在服务器上使用多部分上传文件以及其他内容 并获得上传进度,并在适配器中通知特定项目的进度。
答案 0 :(得分:0)
尝试此上传文件
private PrintWriter writer;
private OutputStream outputStream;
public void addFilePart(File uploadFile) throws IOException {
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
String fileName = uploadFile.getName();
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + "file" + "\"; filename=\"").append(fileName).append("\"")
.append(LINE_FEED);
writer.append("Content-Type: " + "application/octet-stream")
.append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();
FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[4096];
int bytesRead;
float bytesWritten = 0;
int totalSize = (int) uploadFile.length();
while ((bytesRead = inputStream.read(buffer)) != -1) {
if (isCanceled)
return;
outputStream.write(buffer, 0, bytesRead);
bytesWritten += bytesRead;
int progress = (int) ((bytesWritten * fileUploadProgress) / totalSize);
progressUpdateListener.onProgressUpdate(progress);// your progress % here
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
}
编辑
public class UploadFileToServer {
private final String boundary;
private static final String LINE_FEED = "\r\n";
private HttpURLConnection httpConn;
private String charset;
private OutputStream outputStream;
private PrintWriter writer;
private ProgressUpdateListener progressUpdateListener;
private Context context;
/**
* This constructor initializes a new HTTP POST request with content type
* is set to multipart/form-data
*
* @param requestURL requestURL
* @param charset charset
* @param boundary boundary
* @throws IOException ioException
*/
public UploadFileToServer(Context context, String requestURL, String charset, String boundary, ProgressUpdateListener progressUpdateListener) throws IOException {
this.context = context;
this.charset = charset;
this.progressUpdateListener = progressUpdateListener;
// creates a unique boundary based on time stamp
this.boundary = boundary;
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
isCanceled = false;
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
}
/**
* Adds a upload file section to the request
*
* @param uploadFile a File to be uploaded
* @throws IOException ioException
*/
public void addFilePart(File uploadFile)
throws IOException {
String fileName = uploadFile.getName();
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + "file" + "\"; filename=\"").append(fileName).append("\"")
.append(LINE_FEED);
writer.append("Content-Type: " + "application/octet-stream")
.append(LINE_FEED);
// writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();
FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[4096];
int bytesRead;
float bytesWritten = 0;
int totalSize = (int) uploadFile.length();
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
bytesWritten += bytesRead;
int progress = (int) ((bytesWritten * fileUploadProgress) / totalSize);
progressUpdateListener.onProgressUpdate(progress);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
}
public interface ProgressUpdateListener {
void onProgressUpdate(int progress);
}
}
现在您可以像这样调用此类
UploadFileToServer uploadFileToServer = new UploadFileToServer(context, url, "UTF_8", boundary,
new UploadFileToServer.ProgressUpdateListener() {
@Override
public void onProgressUpdate(int progress) {
Log.e("Progress", "onProgressUpdate: " + progress );
}
});
uploadFileToServer.addFilePart(new File(filePath));
答案 1 :(得分:0)
尝试一下
RequestBody videoBody = RequestBody.create(MediaType.parse("video/*"), file);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "name", videoBody)
.build();
requestBuilder = new Request.Builder()
.url(mainUrlString)
.post(requestBody)
.build();