Solved

Write uploaded image to a url using Java

Posted on 2011-02-18
4
600 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 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to implement Singleton Design Pattern in Java.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now