Solved

Write uploaded image to a url using Java

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse with gitlab 1 43
The Best Website Creation Tools 14 55
maven module vs maven project 3 23
Looking for a solution or even half a solution for custom web portal 5 47
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
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 …
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

773 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