Solved

Write uploaded image to a url using Java

Posted on 2011-02-18
4
604 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

733 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