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

  • Home
  • SEARCH
  • 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 919531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:33:39+00:00 2026-05-15T18:33:39+00:00

I have a java servlet which accepts an image a user uploads to my

  • 0

I have a java servlet which accepts an image a user uploads to my web app.

I have another server (running php) which will host all the images. How can I get an image from my jsp server to my php server? The flow would be something like:

public class ServletImgUpload extends HttpServlet 
{   
    public void doPost(HttpServletRequest req, HttpServletResponse resp) 
      throws ServletException, IOException 
    {
        // get image user submitted
        // try sending it to my php server now
        // return success or failure message back to user
    }
}

Thanks

  • 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-15T18:33:40+00:00Added an answer on May 15, 2026 at 6:33 pm

    First of all, why don’t you just submit the form directly to that PHP script?

    <form action="http://example.com/upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit">
    </form>
    

    If this is somehow not an option and you really need to submit the form to the servlet, then first create a HTML form like following in the JSP:

    <form action="upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit">
    </form>
    

    In the servlet which listens on an url-pattern of /upload, you have 2 options to handle the request, depending on what the PHP script takes.

    If the PHP script takes the same parameters and can process the uploaded file the same way as the HTML form has instructed the servlet to do (I would still rather just let the form submit directly to the PHP script, but anyway), then you can let the servlet play for a transparent proxy which just transfers the bytes immediately from the HTTP request to the PHP script. The java.net.URLConnection API is useful in this.

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL("http://example.com/upload.php").openConnection();
        connection.setDoOutput(true); // POST.
        connection.setRequestProperty("Content-Type", request.getHeader("Content-Type")); // This one is important! You may want to check other request headers and copy it as well.
    
        // Set streaming mode, else HttpURLConnection will buffer everything in Java's memory.
        int contentLength = request.getContentLength();
        if (contentLength > -1) {
            connection.setFixedLengthStreamingMode(contentLength);
         } else {
            connection.setChunkedStreamingMode(1024);
        }
    
        InputStream input = request.getInputStream();
        OutputStream output = connection.getOutputStream();
        byte[] buffer = new byte[1024]; // Uses only 1KB of memory!
        for (int length = 0; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
        output.close();
    
        InputStream phpResponse = connection.getInputStream(); // Calling getInputStream() is important, it's lazily executed!
        // Do your thing with the PHP response.
    }
    

    If the PHP script takes different or more parameters (again, I would rather just alter the HTML form accordingly so that it can directly submit to the PHP script), then you can use use Apache Commons FileUpload to extract the uploaded file and Apache HttpComponents Client to submit the uploaded file to the PHP script as if it’s submitted from a HTML form.

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        InputStream fileContent = null;
        String fileContentType = null;
        String fileName = null;
    
        try {
            List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
            for (FileItem item : items) {
                if (!item.isFormField() && item.getFieldName().equals("file")) { // <input type="file" name="file">
                    fileContent = item.getInputStream();
                    fileContentType = item.getContentType();
                    fileName = FilenameUtils.getName(item.getName());
                    break; // If there are no other fields?
                }            
            }
        } catch (FileUploadException e) {
            throw new ServletException("Parsing file upload failed.", e);
        }
    
        if (fileContent != null) {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://example.com/upload.php");
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("file", new InputStreamBody(fileContent, fileContentType, fileName));
            httpPost.setEntity(entity);
            HttpResponse phpResponse = httpClient.execute(httpPost);
            // Do your thing with the PHP response.
        }
    }
    

    See also:

    • How to fire HTTP requests using URLConnection
    • How to upload files in JSP/Servlet
    • HttpClient tutorial
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 462k
  • Answers 462k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just change your code to this <!DOCTYPE html PUBLIC "-//W3C//DTD… May 16, 2026 at 12:18 am
  • Editorial Team
    Editorial Team added an answer Without seeing the screenshots, it's really difficult to say. But… May 16, 2026 at 12:18 am
  • Editorial Team
    Editorial Team added an answer Window form applications generally refers to .NET (C#, VB.NET, etc).… May 16, 2026 at 12:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.