123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package com.example.myapplication.utils;
- import android.util.Log;
- import com.orhanobut.logger.Logger;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.UUID;
- public class Upload extends Thread {
- String url;
- String filename;
- public String toKen;
- String upLoadFileName;
- public Upload(String url, String filename, String toten, String netFileName) {
- super();
- this.url = url;
- this.filename = filename;
- this.toKen = toten;
- this.upLoadFileName = netFileName;
- }
- /**
- * 写表单
- */
- public void doPostUrl() {
- String end = "\r\n";
- String prefix = "--";
- String boundary = UUID.randomUUID().toString(); // 边界标识 随机生成
- try {
- DataOutputStream out = null;
- FileInputStream fStream = null;
- BufferedReader reader = null;
- URL mUrl = new URL(url);
- try {
- HttpURLConnection con = (HttpURLConnection) mUrl
- .openConnection();
- /* 设定传送的method=POST */
- con.setRequestMethod("POST");
- /* setRequestProperty */
- con.setRequestProperty("Connection", "Keep-Alive");
- con.setRequestProperty("Charset", "UTF-8");
- con.setRequestProperty("auth-token", toKen);// took
- con.setRequestProperty("Content-Type",
- "multipart/form-data;boundary=" + boundary);
- /* 设定DataOutputStream */
- out = new DataOutputStream(
- con.getOutputStream());
- out.writeBytes(prefix + boundary + end);
- out.writeBytes("Content-Disposition: form-data; " + "name="
- + upLoadFileName + ";filename=" + "/img.png"
- + end);
- out.writeBytes(end);
- /* 取得文件的FileInputStream */
- fStream = new FileInputStream(
- new File(filename));
- /* 设定每次写入1024bytes */
- int bufferSize = 1024;
- byte[] buffer = new byte[bufferSize];
- int length = -1;
- /* 从文件读取数据到缓冲区 */
- while ((length = fStream.read(buffer)) != -1) {
- /* 将数据写入DataOutputStream中 */
- Log.d("111", String.valueOf(length));
- out.write(buffer, 0, length);
- }
- out.writeBytes(end);
- out.writeBytes(prefix + boundary + prefix + end);
- out.flush();
- // int res = con.getResponseCode();
- reader = new BufferedReader(
- new InputStreamReader(con.getInputStream()));
- StringBuffer sb = new StringBuffer();
- String str;
- while ((str = reader.readLine()) != null) {
- sb.append(str);
- }
- if (mFinishListener != null) {
- mFinishListener.onFinish(sb.toString());
- }
- Logger.d("str: " + sb.toString());
- } catch (IOException e) {
- if (mFinishListener != null) {
- mFinishListener.onFinish(null);
- }
- } finally {
- /* close streams */
- try {
- if (out != null) {
- out.close();
- }
- if (fStream != null) {
- fStream.close();
- }
- if (reader != null) {
- reader.close();
- }
- } catch (IOException e) {
- }
- }
- } catch (MalformedURLException e) {
- if (mFinishListener != null) {
- mFinishListener.onFinish(null);
- }
- }
- }
- private OnUploadFinishListener mFinishListener;
- public void setOnUploadFinishListener(OnUploadFinishListener listener) {
- this.mFinishListener = listener;
- }
- @Override
- public void run() {
- super.run();
- doPostUrl();
- }
- public interface OnUploadFinishListener {
- void onFinish(String message);
- }
- }
|