Android客户端解析非ASCII字符的多部分表单数据

6

您能解释一下为什么Android客户端将多部分数据解析为非ASCII字符,而使用Postman上传文件却可以正常工作吗?这是我的app.js代码:

var multipart = require('connect-multiparty');
var apiRoutes = require('./routes/apiRoutes');
app.set('views', path.join(__dirname, 'views'));
app.use(logger('dev'));
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded([{extended: false},{ uploadDir:path.join(__dirname, 'uploads') }, {parameterLimit:100000}, {limit: '50mb'}]));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'uploads')));
app.use(multipart());
app.use(apiRoutes);

apiRoutes指向我的上传函数,包含简单的打印req参数。在使用postman时它运行良好。

console.log("mediaChat called", req.body, req.files);

响应

mediaChat called { apiKey: '123' } { media: 
   { fieldName: 'media',
     originalFilename: 'default.png',
     path: '/tmp/KFnwsKGp-f4woTaBH6aPR-qa.png',
     headers: 
      { 'content-disposition': 'form-data; name="media"; filename="default.png"',
        'content-type': 'image/png' },
     size: 716,
     name: 'default.png',
     type: 'image/png' } }

这是我的安卓客户端代码(请注意,此代码可以与php $_FILE一起正常工作,但不能与express一起正常工作)

com.info.acruss.wave;

import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Naitik on 9/20/2016.
 */
public class UploadImage extends AsyncTask<Void, Void, String> {
    String CallingURL;
    URL url;
    OnTaskCompleted myListener;
    private static final String TAG = "UploadImage";
    int timeoutMilli = 60000;
    String sourceFileUri;
    String ApiKey,Type;

    public UploadImage(String sourceFileUri, String URL,String apiKey, String type,
                       OnTaskCompleted listener) {
        Log.e("Uploading", "API:" + URL);
        this.sourceFileUri = sourceFileUri;
        this.CallingURL = URL;
        this.myListener = listener;
        this.ApiKey=apiKey;
        this.Type=type;
        try {
            url = new URL(CallingURL);
            Log.e(TAG, "Url : " + CallingURL);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected String doInBackground(Void... params) {
        String fileName = sourceFileUri;
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 10 * 1024 * 1024;
        File sourceFile = new File(sourceFileUri);
        if (!sourceFile.isFile()) {
            Log.e("UploadImage", "Source File Does not exist";
            return null;
        }
        String serverResponseMessage = "";
        try {

            // open a URL connection to the Servlet
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            URL url = new URL(CallingURL);
            // Open a HTTP  connection to  the URL
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            conn.setUseCaches(false); // Don't use a Cached Copy
            conn.setRequestMethod("POST";
            conn.setReadTimeout(timeoutMilli);
            conn.setConnectTimeout(timeoutMilli);
            conn.setRequestProperty("Connection", "Keep-Alive";
            conn.setRequestProperty("ENCTYPE", "multipart/form-data";
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("media", fileName);
            conn.setRequestProperty("apiKey",ApiKey);
            conn.setRequestProperty("media_type",Type);
            conn.setRequestProperty("media", fileName);

            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name='media';filename='" + fileName + "'" + lineEnd);
               dos.writeBytes(lineEnd);
            // create a buffer of  maximum size
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {

                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            }

            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            int serverResponseCode = conn.getResponseCode();

            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = "";
            StringBuilder responseOutput = new StringBuilder();
            while ((line = br.readLine()) != null) {
                responseOutput.append(line);
            }
            br.close();
            serverResponseMessage = responseOutput.toString();//output.toString();

            Log.e("uploadFile", "HTTP Response is : " + serverResponseMessage);
            if (serverResponseCode == 200) {
                //status code 200
                //status ok
            }
            //close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
        } catch (Exception e) {
            Log.e("Upload file to server", "error: " + e.getMessage(), e);
            e.printStackTrace();
        }
        return serverResponseMessage;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Log.e("Result", "" + result);
        if (myListener != null)
            if (result != null) {
                myListener.onFileUploadComplete(result);
            } else {
                myListener.onFileUploadComplete("";
            }
    }

    public interface OnTaskCompleted {
        void onFileUploadComplete(String result);
    }
}

使用Android时,以下是显示有线响应的方法:

mediaChat called { null: '����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000C\u0000\u0010\u000b\f\u000e\f\n\u0010\u000e\r\u000e\u0012\u0011\u0010\u0013\u0018(\u001a\u0018\u0016\u0016\u00181#%\u001d(:3=Mqypdx\\egc��\u0000C\u0001\u0011\u0012\u0012\u0018\u0015\u0018/\u001a\u001a/cB8Bccccccccccc....
�\u001f.[���_�\u0014)M���XIjX��7�`=�/�8`��ïDʚ\u0018�D���#�V#q~m�q10L�' }

我还尝试了multer和其他multipart处理程序,但什么都不起作用。 请帮助我摆脱这个困境。


也许这个链接对你有帮助:https://github.com/koush/ion - Ebrahim Pasbani
3个回答

2

看起来服务器响应已经使用UTF-8编码。为了正确解码和阅读,您可以尝试以下方法:

BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

将编码部分指定给InputStreamReader将使用指定的编码方案解码和读取流,这种情况下的编码方案是UTF-8。有关详细信息,请参阅javadocs


0

多部分表单提交消除了百分号编码的许多歧义:服务器现在可以明确要求某些编码,客户端可以在表单提交期间明确告诉服务器字段所在的编码。

有两种方法可以使用此功能:将其保留未设置,并使浏览器以与页面相同的编码发送,或将其设置为UTF-8,然后在服务器端进行另一次转换。每种方法都有缺陷,特别是前者。

如果告诉浏览器以与页面相同的编码发送表单,则仍然存在如何处理超出字符编码范围的字符的问题。行为再次因浏览器而异:Firefox 2.0将它们转换为字符实体引用,而Internet Explorer 7.0则将它们搞得难以理解。对于严肃的国际化目的,这不是一个选项。

另一种可能性是将Accept-Encoding设置为UTF-8,这引发了一个问题:为什么不对所有内容都使用UTF-8?这条路更容易接受,但有一个值得注意的警告:您的数据将以UTF-8格式传入,因此您必须明确地将其转换为您喜欢的本地字符编码。

基于Unicode的编码,如UTF-8,可以支持许多语言,并且可以容纳任何这些语言的页面和表单的混合。它的使用还消除了服务器端逻辑来单独确定为每个服务的页面或每个传入表单提交的字符编码的需要。这显着减少了处理多语言站点或应用程序的复杂性。

Unicode编码还允许在单个页面上混合许多更多的语言,而不是其他任何编码选择。

请查看为什么使用UTF-X


Android上的响应显示了UTF字符代码。这意味着服务器实际上正在使用UTF编码来编写响应。问题在于客户端函数读取数据时没有使用UTF进行解码。如果您只看一下代码 - Siddharth Tyagi

0

根据新的bufferSize大小,您需要重新分配缓冲区,我已经修改了下面的代码:

// create a buffer of  maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0) {

    dos.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);

    buffer = null;
    buffer = new byte[bufferSize];

    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

}

显然这是编码问题,而不是从服务器读取的部分响应。这是一个不恰当的修复方法。 - Siddharth Tyagi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接