Android Upload File to Server

鲜于致远
2023-12-01

提交本地文件到服务器,这里仅仅提供一个代码,解释等后期有时间了慢慢写。

class UploadTask extends AsyncTask<String, Integer, String> {

    public UploadTask() {
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        for (int i = 0; i < params.length; i++) {
            String path = params[i];
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "******";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1* 1024 * 1024;
            File imgFile = new File(path);
            if (!imgFile.isFile()) {
                return null;
            } else {
                try {
                    FileInputStream fis = new FileInputStream(imgFile);
                    URL url = new URL(UPLOAD_URL);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    conn.setUseCaches(false);
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                    conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
                    conn.setRequestProperty("uploaded_file", path);

                    dos = new DataOutputStream(conn.getOutputStream());

                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\'upload_file\';filename=" + path + lineEnd);

                    dos.writeBytes(lineEnd);

                    bytesAvailable = fis.available();

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    buffer = new byte[bufferSize];

                    bytesRead = fis.read(buffer, 0, bufferSize);
                    while (bytesRead > 0) {
                        dos.write(buffer, 0, bufferSize);

                        bytesAvailable = fis.available();
                        bufferSize = Math.min(bytesAvailable, maxBufferSize);
                        bytesRead = fis.read(buffer, 0, bufferSize);
                    }

                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                    int responseCode = conn.getResponseCode();

                    String responseMessage = conn.getResponseMessage();
                    if (responseCode == 200) {
                        LogMgr.i(TAG, responseMessage);
                    }
                    fis.close();
                    dos.flush();
                    dos.close();
                } catch (Exception e) {
                    return "error";
                }
            }
        }
        return "Success";
    }

    @Override
    protected void onPostExecute(String result) {
        Log.e(TAG, "[onPostExecute] " + result);
    }

}
 类似资料:

相关阅读

相关文章

相关问答