?
Solved

Write uploaded image to a url using Java

Posted on 2011-02-18
4
Medium Priority
?
615 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
  • 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 375 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
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 is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

840 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