Send image to servlet

How to send image to servlet over HttpURLConnection

Page 1 of 1

3 Replies - 13797 Views - Last Post: 10 March 2010 - 11:38 AM

#1 Vikito  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-April 09

Send image to servlet

Post icon  Posted 07 December 2009 - 09:58 AM

Hi, i have this problem and i can't solve it for long time. Please if somebody can help. I have a client program that is written in java. This client must take image from the hard disk and send it to servlet. I read about this and everybody says that it is simple, but i am still struggling. So i must send the image trough the OutputStream of the HttpURLConnection. This is what i do:
try
		{
			HttpURLConnection connection  = (HttpURLConnection)url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestProperty("Content-type", "image/jpeg");
			connection.setRequestMethod("POST");

			File imageFile = new File("path to the image on the hdd");
			FileInputStream fInput = new FileInputStream(imageFile);

			OutputStream out = connection.getOutputStream();
			DataOutputStream dataOut = new DataOutputStream(out);

			while(fInput.available()>0)
			{
				int buffer = fInput.read();
				System.out.println("Read from the image: "+buffer);
				dataOut.writeInt(buffer);
			}
			fInput.close();
			dataOut.close();

		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}


on the other side - the doPost method of the servlet gets the inputstream of the HttpServletRequest and try to read it but nothing happens. The doPost method for this request is never called.
May be the problem is in the outputstream(i guess i should use differen outputstream). May be i'm not sending the image right.
Please help!

Is This A Good Question/Topic? 0
  • +

Replies To: Send image to servlet

#2 Programmist  Icon User is offline

  • Refactorer in Chief
  • member icon

Reputation: 255
  • View blog
  • Posts: 1,843
  • Joined: 02-January 06

Re: Send image to servlet

Posted 08 December 2009 - 05:51 AM

Try Apache HttpComponents Client for the client side code and Apache Commons FileUpload for the Servlet side reading the upload. I've successfully used these in t he past to do exactly what you're trying to do.
Was This Post Helpful? 1
  • +
  • -

#3 Vikito  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-April 09

Re: Send image to servlet

Posted 09 December 2009 - 07:37 PM

Thank you, very much, Programmist! I haven't applied what you suggest yet but i think this is exactly what i need! This is such a relief! Thank you again!
Was This Post Helpful? 0
  • +
  • -

#4 Vikito  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-April 09

Re: Send image to servlet

Posted 10 March 2010 - 11:38 AM

i am trying to use these but i recieve an error : org.apache.commons.fileUpload.FileUploadException: the request was rejected because no multipart boundary was found.

I tried to find the answer of this in the httpclient tutorial but nothing about setting boundary. How do i set boundary? And why do i need it if i only want to send a file?

here it is my code for the client:
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.*;
import org.apache.http.*;
import org.apache.http.params.BasicHttpParams;
import java.util.*;
import org.apache.http.entity.*;
import org.apache.http.params.DefaultedHttpParams;


public class ImageClient
{
	public static void main(String[] args)
	{
		HttpClient httpclient = new DefaultHttpClient();
		File file = new File("imagePath\image.jpg");
		FileEntity entity = new FileEntity(file, "multipart/form-data");
		HttpPost httppost = new HttpPost("http://localhost/diplomna/ImageServlet");
		httppost.setEntity(entity);
		try
		{
			HttpResponse response = httpclient.execute(httppost);
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}

	}
}




and this is what i do on the server side:

import java.io.*;
import java.lang.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;


public class ImageServlet extends HttpServlet
{

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("Vlizam doPost()");

		FileItemFactory factory = new DiskFileItemFactory();

		// Create a new file upload handler
		ServletFileUpload upload = new ServletFileUpload(factory);
		// Parse the request
		try
		{
			List /*FileItem*/  items = upload.parseRequest(request);

			// Process the uploaded items
			Iterator iter = items.iterator();
			while (iter.hasNext())
			{
		    	FileItem item = (FileItem) iter.next();

		    	if (item.isFormField())
		    	{
					processFormField(item);
		    	}
		    	else
		    	{
		    	    processUploadedFile(item);
		    	}
			}

		}
		catch(FileUploadException fupe)
		{
			fupe.printStackTrace();
		}

	}

	private void processUploadedFile(FileItem item)
	{
		if (!item.isFormField())
		{
		    String fieldName = item.getFieldName();
		    String fileName = item.getName();
		    String contentType = item.getContentType();
		    boolean isInMemory = item.isInMemory();
		    long sizeInBytes = item.getSize();
		    System.out.println("FileName: "+ fileName);
		}
        }
}




I do realize that what i send to the servlet is not multipart/form-data but when i change the data type the server says it is looking for multipart/form-data.

Can't i use httpclient to send only file not multipart/form-data? If it's not possible how to set this stupid boundary?
If somebody knows the answers, please help!
Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1