Results 1 to 5 of 5
  1. #1
    saisakthi is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Question Image upload from file system to local host (server) through http protocol in java

    I need to upload my image to localhost(I mean, I need to specify the destination location in terms of http like http://localhost:8080/somelocation). I need code for this in java.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    This is a forum, not a code mill. Post your best efforts and ask a specific question, and there are many here who will try to help you.

    db

  3. #3
    saisakthi is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Sorry! I am very new to java. I just followed the thread (Upload an image.) I didnt got errors, it seems to be working fine. But I didnt got the image copied in the destination. So can u please guide me in particular direction.

  4. #4
    saisakthi is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Hi this is my code, I am trying to send image in my filesystem to tomcat server running at port 8080. While running this code, I am not getting any exception. But I didnt got the image in my destination.


    String uploadUrl = "http://localhost:8080/images";
    String targetFileName = "blue.jpg";
    URL url;
    HttpURLConnection connection = null;
    try {
    //Create connection
    url = new URL(uploadUrl);
    connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "image/png");
    connection.setRequestProperty("Target-File-Name", targetFileName);
    connection.setUseCaches (false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    //Send request
    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    Image i = Toolkit.getDefaultToolkit().getImage("/blue.jpg");
    BufferedImage bimg = new BufferedImage (i.getWidth(100), i.getHeight(130), BufferedImage.TYPE_INT_ARGB);
    bimg = (BufferedImage)i;
    ImageIO.write(bimg, "png", wr);
    wr.flush ();
    wr.close ();

    //Get Response
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer response = new StringBuffer();
    while((line = rd.readLine()) != null) {
    response.append(line);
    response.append('\r');
    }
    rd.close();

  5. #5
    saisakthi is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. upload file from local system to database
    By harsha.udupa2008 in forum Advanced Java
    Replies: 3
    Last Post: 09-17-2010, 09:58 AM
  2. copy file from local host to remote host
    By isotoper in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 05:41 PM
  3. :large file upload to server(chunk upload)
    By tommy_725 in forum Networking
    Replies: 0
    Last Post: 10-16-2009, 01:21 AM
  4. Replies: 9
    Last Post: 09-01-2009, 08:17 PM
  5. How to read xml file located in local system using Java
    By java_kick in forum Advanced Java
    Replies: 2
    Last Post: 04-07-2009, 12:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •