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 7164563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:05:44+00:00 2026-05-28T14:05:44+00:00

I am trying to upload an image to a PHP page along with some

  • 0

I am trying to upload an image to a PHP page along with some other info about the image so the PHP page knows what to do with it. Currently, I am getting away with it using this:

URL url = new URL("http://www.tagverse.us/upload.php?authcode="+WEB_ACCESS_CODE+"&description="+description+"&userid="+userId);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");

OutputStream os = connection.getOutputStream();
InputStream is = mContext.getContentResolver().openInputStream(uri);
BufferedInputStream bis = new BufferedInputStream(is);
int totalBytes = bis.available();

for(int i = 0; i < totalBytes; i++) {
    os.write(bis.read());
}
os.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String serverResponse = "";
String response = "";

while((response = reader.readLine()) != null) {
    serverResponse = serverResponse + response;
}
reader.close();
bis.close();

Is there a more elegant solution to this besides having a GET/POST hybrid? I feel as if this is sloppy, but for all I know it is a perfectly acceptable solution. If there is a better way of doing this, I’d appreciate being pointed in the right direction. Thanks!

PS: I am familiar with how you would, under normal conditions, interact with a PHP page via POST:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.tagverse.us/login.php");

try {
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("authcode", WEB_ACCESS_CODE));
    pairs.add(new BasicNameValuePair("username", username));
    pairs.add(new BasicNameValuePair("password", password));
    post.setEntity(new UrlEncodedFormEntity(pairs));
    client.execute(post);
}

Essentially what I’d like to do is combine these two methods, but because I work with an HttpURLConnection object rather than a HttpPost object, it isn’t as simple as just merging the two.

Thank you!

  • 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-28T14:05:45+00:00Added an answer on May 28, 2026 at 2:05 pm

    You can try an take a look at the answer I added for this similar question:
    https://stackoverflow.com/a/9003674/472747

    Here is the code:

    byte[] data = {10,10,10,10,10}; // better get this from a file or memory
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost("server url");
    ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    reqEntity.addPart("image", bab);
    
    FormBodyPart bodyPart=new FormBodyPart("formVariableName", new StringBody("formValiableValue"));
    reqEntity.addPart(bodyPart);
    bodyPart=new FormBodyPart("formVariableName2", new StringBody("formValiableValue2"));
    reqEntity.addPart(bodyPart);
    bodyPart=new FormBodyPart("formVariableName3", new StringBody("formValiableValue3"));
    reqEntity.addPart(bodyPart); 
    postRequest.setEntity(reqEntity);
    HttpResponse response = httpClient.execute(postRequest);
    BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String line = null;
    while((line = in.readLine()) != null) {
        System.out.println(line);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to upload an image from a jsp page using servlet. but
I'm trying to upload an image from android to a PHP server by using
Hi i'm trying to upload an image using a php script. And whats really
I currently have a PHP script I use to upload image files but I'm
I am trying to secure my PHP Image upload script and the last hurdle
Hi I wanted to upload images(along with other form details) and preview them, using
i am trying to upload a image to server from iPhone application PHP code
hi all i am trying to upload image file from sdcard to php server
I am trying to upload a file using jQuery ajax, but keep getting this
I am using OpenLayers and jQuery Mobile and trying to upload an image to

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.