Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7618985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:32:40+00:00 2026-05-31T03:32:40+00:00

My upload code as below: String end = \r\n; String twoHyphens = –; String

  • 0

My upload code as below:

String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
URL url = new URL(ActionUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Accept", "text/*");
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;" + "name=\"folder\"" + end + end);
ds.write(SavePath.getBytes("UTF-8"));
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;" + "name=\"Filedata\"; filename=\"");
ds.write(FileName.getBytes("UTF-8"));
ds.writeBytes("\"" + end);
ds.writeBytes(end);
FileInputStream fStream = new FileInputStream(uploadFilepath+""+FileName);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
int pro = 0;
while((length = fStream.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}       
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
fStream.close();
ds.flush();
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while((ch = is.read()) != -1) {
b.append((char)ch);
}
ds.close();
}
catch(Exception e) {
e.printStackTrace();
}

It can works while upload a smaller file.
But while more 16 mb, it will upload fail and show the OutOfMemory error.
I think it cause by put all data in buffer.
So I want to make it to send data while buffer save 1024 bytes.
But I no idea to do that.
May anyone help me to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:32:41+00:00Added an answer on May 31, 2026 at 3:32 am

    You should confirm at what point your error occurs. I suspect that it’s during reading the response. In this case, it seems that server may be responding with a lot of data that you place in the StringBuffer. Do you actually need to consume the entire response and keep it in memory? If it’s a file, save it rather than keeping in memory.


    I did some more research and here is one other possibility. Android JVM by default has 16mb max heap. See this for some details.

    On the other hand, if your server does not actually consume the data, most of it will reside on the client. So if you have more than max heap of data the client will fail.

    So I suspect that your server just does not read the data from the stream.

    The following class (which is a snippet of relevant parts of your code) illustrates the problem. Run it on any JVM like following:

    java -Xmx16m -cp . Test
    

    and it will produce OOM very quickly. In fact, much earlier than expected.

    import java.io.*;
    import java.net.*;
    
    public class Test {
      public static void main(String argv[]) throws Exception {
        new Thread() {
          public void run() {
            try {
               new ServerSocket(12000).accept();
            } catch (Throwable t) {
              t.printStackTrace();
            } 
          }
        }.start();
    
        URL url = new URL("http://localhost:12000/");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        DataOutputStream ds = new DataOutputStream(con.getOutputStream());
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];
        for (int i=0;i<100000;i++) {
          ds.write(buffer, 0, 1024);
          System.out.println("Written chunk " + i);
        }       
        ds.flush();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My upload code as below: String end = \r\n; String twoHyphens = --; String
I am using the below code to fire an event when the upload queue
hi thanks a lot i have one code below in this code ill upload
I'm using a code below to upload file (tens of MB) in streaming mode.
I am new to JSP and I am trying to write the below code:
I have an application were users can upload pics. The code below allow only
I am using the code below to upload a file. The files upload without
I have the below code. What happens if the Upload method takes more than
I need to upload a bitmap to Amazon S3. Below is the code I
Hello i have the code below to upload pictures for my application. I will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.