如何在没有内存错误的情况下将文件上传到服务器?

时间:2015-10-29 08:47:34

标签: android

你好朋友我试图在服务器上传文件,但我的文件大小很大,我的内存出错是我的上传代码

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

    String currentUrl = OnlineConvert.URL;
    String xmlResponse = "";
    int maxbuffersize = 212144;

    Log.d("main", "upload start   ");
    /*
     * if (!"get-queue".equals(action)) {
     * this.getServer(this.targetTypeOptions.get(this.targetType)); }
     */
    try {
        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.setChunkedStreamingMode(maxbuffersize);
        conn.setDoInput(true);

        OutputStream wr = conn.getOutputStream();
        wr.write(params.getBytes());

        /*bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];*/
        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;
}

任何人都可以帮我解决这个问题吗?在不同的设备中,文件的大小是不同的。我不知道什么是实际问题

1 个答案:

答案 0 :(得分:0)

缩小图像,因为图像的字节数组太大。