0 Replies Latest reply: Jun 17, 2009 7:48 AM by 843851 RSS

    Why is this working?

    843851
      I have my program running, but i think it's a fluke. I can't figure out why this works"
      {code}
      public FileUploader(ImagePlus imp) {
                this.imp = imp;
      fi = imp.getFileInfo();
      try {
      //Construct a URL refering to the servlet
      URL url = new URL(BASE + "/HRH");
      URLConnection connection = url.openConnection();
      connection.setUseCaches(false);
      connection.setDoOutput(true);
      connection.setDoInput(true);
      connection.connect();
      OutputStream os = connection.getOutputStream();

      upload(os);
      is = connection.getInputStream();

      } catch (Exception e){
      IJ.error("Error: URL connection Failed.");
      }
           }

      public void upload(OutputStream os){

      try{
      // Create a temporary file object
      File tempFile = File.createTempFile(fi.fileName, "tif");
      TiffEncoder file = new TiffEncoder(fi);
      DataOutputStream out = new DataOutputStream
      (new BufferedOutputStream
      (new FileOutputStream(tempFile)));
      file.write(out);
      out.flush();
      out.close();

      ObjectOutputStream oos = new ObjectOutputStream(os);
      IJ.showMessage("oos: "+ oos.toString());
      IJ.showMessage("tempFile size: " +
      Integer.toString((int)tempFile.length()));
      oos.writeObject(tempFile);
      oos.flush();
      oos.close();

      //delete local copy of file now that it has been outputed
      //tempFile.delete();

      /*/Verify the file has been written
      DataInputStream result =
      new DataInputStream(new BufferedInputStream(is));
      String response = result.readLine();
      IJ.showMessage("got here");
      if (!response.equals("") && !response.equals(null)){
      IJ.showMessage(response);
      }*/
      //is.close();

      }catch(Exception e){
      IJ.error("Error: Unable to complete file transfer.");
      }
      }
      {code}

      if I change:
      {code}
      upload(os);
      is = connection.getInputStream();
      {code}
      to:
      {code}
      is = connection.getInputStream();
      upload(os);
      {code}

      it no longer works i.e. the file doesn't transfer to the server. However in the first example it works just fine. I don't quite understand what's going on here.

      -Seth

      Edited by: shays on Jun 17, 2009 5:47 AM

      Edited by: shays on Jun 17, 2009 5:48 AM