【问题标题】:in android how can i upload large size of file on server by using API?在 android 中,如何使用 API 在服务器上上传大文件?
【发布时间】:2015-11-28 18:18:50
【问题描述】:

嗨朋友们,我正在使用 Web API 制作文件转换应用程序。为此我需要将我的文件发送到服务器我的代码完全运行我能够将文件发送到服务器但是当我选择大文件时它显示内存异常上传文件的代码在这里

private String apiCall(String action, List query, String formatOptionXml)
        throws Exception {

    String currentUrl = OnlineConvert.URL;
    String xmlResponse = "";

    Log.d("main", "upload start   ");
    /*
     * if (!"get-queue".equals(action)) {
     * this.getServer(this.targetTypeOptions.get(this.targetType)); }
     */
    try {
        //FileInputStream fi = new FileInputStream(new File(in.inputpath));
        URL browser = new URL(currentUrl + "/" + action);
        HttpURLConnection conn = (HttpURLConnection) browser.openConnection();
        String params = "queue=" + URLEncoder.encode(this.getMap2XML((Map) query.get(OnlineConvert.QUEUE_COMMAN_PARAMS),"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",formatOptionXml+ this.getFileMap2XML((Map) query.get(OnlineConvert.QUEUE_FILE_METADATA_PARAMS))),"UTF-8");
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length",String.valueOf(params.length()));
        conn.setRequestProperty("connection", "close");
        conn.setUseCaches(false);
        conn.setDoOutput(true);
        conn.setDoInput(true);

        OutputStream wr = conn.getOutputStream();
        wr.write(params.getBytes());
        wr.flush();
        // Get the response
        int statusCode = conn.getResponseCode();
        BufferedReader in = null;
        String inputLine;
        StringBuilder response = new StringBuilder();
        if (statusCode >= 200 && statusCode < 400) {
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
        } else {
            in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
        }

        xmlResponse = response.toString();

        wr.close();
        in.close();

    } catch (MalformedURLException e) {
        Log.d("main", "MalformedURLException   " + e);
        throw new MalformedURLException(
                "Error downloading pathway overview images :"
                        + e.getMessage());
    } catch (IOException e) {
        Log.d("main", "IOException  " + e);
        throw new IOException("Error In IOException :" + e.getMessage());
    } catch (Exception e) {
        Log.d("main", "Exception " + e);
        throw new Exception("Exception :" + e.getMessage());
    }
    Log.d("main", "upload done");
    return xmlResponse;
}

这样我可以上传小文件,但是当我尝试发送超过 10MB 的文件时,它会显示内存异常,所以请帮我解决这个问题

【问题讨论】:

标签: android


【解决方案1】:

我认为您可以使用多部分连接请求发送此内容并更改您的代码。

connection.setRequestProperty("内容类型", "multipart/form-data;boundary=" + 边界);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-06
  • 2014-07-12
  • 2012-03-09
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
  • 2012-04-23
相关资源
最近更新 更多