2012-03-12 97 views
3

我对android开发非常陌生,我试图上传25到50 MB大小的文件到Web服务器,并且出现内存不足错误。我在过去2天挣扎,没有线索,我错了。Android上传大文件

有什么建议我要去哪里出错?

我工作的代码是

private FileInputStream fileInputStream = null; 
private int bytesavailable,buffersize,bytesRead ; 
byte buff[]; 
int maxBufferSize = 1*1024*1024; 
String server_url ="server_url"; 
DataOutputStream dos; 
String Builder response = new String Builder(); 
String body = "boundary values"; 
String body2 = "Boundary values"; 
URL url = null; 
    try 
    { 
     url = new URL(server_url); 
    } 
    catch (MalformedURLException e1) 
    { 
     e1.printStackTrace(); 
    } 

    HttpURLConnection conn = null; 
    try 
    { 
     conn = (HttpURLConnection) url.openConnection(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    try 
    { 
     conn.setRequestMethod("POST"); 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 
     conn.setRequestProperty("Connection","Keep-Alive"); 
     conn.setRequestProperty("Content-Type","multipart/form-data; boundary=A300x"); 
     conn.connect(); 
     dos = new DataOutputStream(conn.getOutputStream()); 
     dos.writeBytes(body); 
     File inputfile = new File(sourceFile); 
     fileInputStream = new FileInputStream(inputfile); 


     bytesavailable = fileInputStream.available(); 
     buffersize = Math.min(bytesavailable, maxBufferSize); 
     buff = new byte[buffersize]; 
     bytesRead = fileInputStream.read(buff, 0, buffersize); 


      while (bytesRead > 0) 
      { 

       dos.write(buff, 0, buffersize); 
       dos.flush(); 
       bytesavailable = fileInputStream.available(); 
       buffersize = Math.min(bytesavailable, maxBufferSize); 
       bytesRead = fileInputStream.read(buff, 0, buffersize); 
      } 
     fileInputStream.close(); 
     dos.write("\r\n".getBytes()); 
     dos.write(body2.getBytes()); 
     dos.flush(); 
     dos.close(); 
} 
    catch (ProtocolException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    int iresponse = 0; 
    try 
    { 
     iresponse = conn.getResponseCode(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    //printStream(conn.getInputStream()); 

    if (iresponse == HttpURLConnection.HTTP_OK) 
    { 

     BufferedReader input = null; 
     try 
     { 
      input = new BufferedReader(new InputStreamReader(conn.getInputStream()), 8192); 
     } 
     catch (IOException e1) 
     { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     String line = null; 
     try 
     { 
      while ((line = input.readLine()) != null) 
       response.append(line); 
     } 
     catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try 
     { 
      input.close(); 
     } 
     catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 


    return response.toString(); 
+0

你可以发布你的堆栈跟踪吗?它看起来像你在上传过程中传输输入文件。 – louielouie 2012-03-12 06:09:21

回答

0

不能将整个文件加载到内存 - 在许多设备上,你只限于16MB或堆的32MB。您应该以小块将文件流式传输到服务器。

+0

是否有任何示例程序解释如何分块数据? – 2012-03-12 00:53:19

+1

http://stackoverflow.com/questions/9630430/upload-large-file-in-android-with-outofmemory-error - 检查这个网址 – 2012-05-11 06:22:48

0

这可能对您有帮助。

private void uploadFile(File file) throws IOException { 
    Log.i(TAG, "Uploading " + file); 
    String videoName = file.getParentFile().getName(); 

    AndroidHttpClient httpclient = AndroidHttpClient.newInstance(GoProLive.TAG); 
    try { 
     HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), ConnectTimeout); 
     HttpConnectionParams.setSoTimeout(httpclient.getParams(), DataTimeout); 
     HttpPost post = new HttpPost(
       String.format("http://" + ServerName + "/upload/%s/%s", user.getUsername(), file.getName())); 
     post.setEntity(new FileEntity(file, "application/octet-stream")); 

     SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT, Locale.US); 
     df.applyPattern("EEE, dd MMM yyyy HH:mm:ss z"); 
     post.setHeader("Last-Modified", df.format(new Date(file.lastModified()))); 
     HttpResponse httpResponse = executePost(httpclient, post); 
     int statusCode = httpResponse.getStatusLine().getStatusCode(); 
     if (statusCode == HttpStatus.SC_OK) { 
      file.delete(); 
     } else { 
      throw new HttpException("Failed to upload file " + file.getAbsolutePath(), statusCode); 
     } 
    } finally { 
     httpclient.close(); 
    } 
}