Solved

Write uploaded image to a url using Java

Posted on 2011-02-18
4
605 Views
Last Modified: 2013-12-20
Hi,

I want to write an uploaded image to a URL in Java. I was able to successfully create a URL connection and pass the data to the outputstream (code below). But the problem is that I dont see the image in the folder on that URL. I read in multiple places that some code is needed on the server side to accept images. What kind of code is needed on the server? Please help.


                  URL url = new URL("http://aaaa.edu/xyz");
                  HttpURLConnection con = (HttpURLConnection) url.openConnection();
                  con.setDoOutput(true);
                  con.setUseCaches(false);
                  con.setRequestProperty("Content-Type", "image/jpeg");
                  con.setRequestMethod("POST");
                  OutputStream out = con.getOutputStream();

                        InputStream in = item.getInputStream();
                  byte[] buffer = new byte[12288]; // 12K
                    long count = 0L;
                    int n = 0;
                  while (-1 != (n = input.read(buffer))) {
                  System.out.println("buffer = "+buffer);
                  output.write(buffer, 0, n);
                  count += n;
                  }
                  out.flush();
                  out.close();

0
Comment
Question by:MeetMahek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:brain_box
ID: 34931764
You have to write a Servlet which can handle the stream and do the required.(Store it on the server in this case).It is however, not advisable(if allowed at all) to access the local file system on the server from the servlet. A better way will be to store it in a database server. Writing a servlet on the server and hosting is pretty basic and you will find many examples of it over internet.You can test it initially locally on tomcat(or any other servlet container). Contact your website admin ("http://aaaa.edu/xyz") for hosting your servlet on the server. Please note that the website may not be hosted on Java Application Server. In which case, you have to use corresponding technology to handle the post request.
0
 

Author Comment

by:MeetMahek
ID: 34933637
As a follow-up, Is there a way to save on the server file system without writing a servlet? It will be very difficult for me to get access to the server.
0
 
LVL 2

Accepted Solution

by:
brain_box earned 125 total points
ID: 34933766
No MeetMahek, You have to contact the server admin to confirm if they have any such facility available. By default most of the administrators do not allow these services due to security concerns. I will suggest using a ftp based service. If you are not tied to the service provider, there are certain free image hosting services which provide api. A simple search on google will return many such websites. imgur is one such service. Visit imgur.com and http://code.google.com/p/imgur-api/wiki/ImageUploading for more details.
0
 

Author Closing Comment

by:MeetMahek
ID: 35047211
I wanted to know other ways of solving this problem
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question