- protected
- String doUpload(HttpURLConnection connection, UploadParam param)
- throws Exception {
- String path
- =
- param.getPath();
- String fileKey
- = TextUtils.isEmpty(param.getFileKey()) ? "file"
- : param.getFileKey();
- String fileName
- =
- param.getFileName();
- String fileType
- = TextUtils.isEmpty(param.getContentType())
- MIME_TYPE_ALL : param.getContentType();
- DataOutputStream outs
- =
- null;
- BufferedReader ins
- =
- null;
- FileInputStream fouts
- =
- null;
- String response
- =
- null;
- try {
- // Content-Disposition: form-data; name="fileKey"; filename="bg_entry.png"
- // Content-Type: image/png
- StringBuilder builder =
- new StringBuilder(buildParams(param.getParams()));
- builder.append(getBoundaryPrefixed())
- .append(CRLF)
- .append(String.format(HEADER_CONTENT_DISPOSITION
- + COLON_SPACE + FORM_DATA + SEMICOLON_SPACE +
- FILENAME, fileKey, fileName))
- .append(CRLF)
- .append(HEADER_CONTENT_TYPE).append(fileType)
- .append(CRLF)
- //Must jump to new line to indicate the beginning of data.
- .append(CRLF);
- byte
- [] headBuf =
- builder.toString().getBytes(CHARSET_UTF8);
- //Must jump to new line to indicate the end of data.
- byte
- [] tailBuf = (CRLF + getBoundaryPrefixed() + BOUNDARY_PREFIX +
- CRLF).getBytes(CHARSET_UTF8);
- long
- currentBytes = 0
- ;
- File file
- =
- new File(path);
- long
- totalSize = file.length() + headBuf.length +
- tailBuf.length;
- //Generally speaking,Files larger than 4M should use streaming mode.
- if
- (totalSize > 4 * 1024 * 1024
- ) {
- //Avoid oom when post large file.Ether way is ok.
- connection.setChunkedStreamingMode(1024
- );
- // connection.setFixedLengthStreamingMode(totalSize);
- }
- connection.setRequestProperty(HEADER_CONTENT_LENGTH, String.valueOf(totalSize));
- connection.connect();
- outs
- =
- new DataOutputStream(connection.getOutputStream());
- outs.write(headBuf);
- currentBytes
- +=
- headBuf.length;
- updateProgress(currentBytes, totalSize);
- fouts
- =
- new FileInputStream(file);
- byte
- [] buffer =
- new byte
- [1024
- ];
- int
- length = -1
- ;
- long
- startTime =
- System.currentTimeMillis();
- long
- now = 0
- ;
- while
- ((length = fouts.read(buffer)) != -1
- ) {
- if
- (length > 0
- ) {
- outs.write(buffer,
- 0
- , length);
- currentBytes
- +=
- length;
- now
- =
- System.currentTimeMillis();
- if
- (now - startTime >=
- PROGRESS_RATE) {
- updateProgress(currentBytes, totalSize);
- startTime
- =
- now;
- }
- }
- if
- (!
- canRun()) {
- throw new
- Exception("Upload cancelled"
- );
- }
- }
- outs.write(tailBuf);
- outs.flush();
- updateProgress(totalSize, totalSize);
- fouts.close();
- fouts
- =
- null;
- //Response.
- if
- (connection.getResponseCode() != 200
- ) {
- throw new
- IllegalStateException(String.format("Error upload response: code:%s msg:%s"
- , connection.getResponseCode(), connection.getResponseMessage()));
- }
- ins
- =
- new
- BufferedReader(
- new InputStreamReader(connection.getInputStream()));
- String line;
- StringBuffer b
- =
- new StringBuffer();
- while
- ((line = ins.readLine()) !=
- null) {
- b.append(line);
- if
- (!
- canRun()) {
- throw new
- Exception("Upload cancelled"
- );
- }
- }
- response
- =
- b.toString();
- if (TextUtils.isEmpty(response)) {
- throw new
- NullPointerException("Null response: " +
- response);
- }
- outs.close();
- outs
- =
- null;
- ins.close();
- ins
- =
- null;
- } finally {
- if
- (fouts !=
- null) {
- fouts.close();
- fouts
- =
- null;
- }
- if
- (outs !=
- null) {
- outs.close();
- outs
- =
- null;
- }
- if
- (ins !=
- null) {
- ins.close();
- ins
- =
- null;
- }
- }
- return response;
- }
来源: http://www.cnblogs.com/oxgen/p/7189854.html