Results 1 to 3 of 3
  1. #1
    anurit is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default How write created file on remote host without saving on HDD?

    I create PDF or image file in my programm (applet) and I will send this file in e-mail.
    Now I create file, write on my HDD and connect with PHP script and in POST method send file to remote host.
    I will create file without saving him on HDD (I will not use signed applet).
    How have I transform following code:

    create image:
    BufferedImage bim = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
    this.paintAll(bim.getGraphics());
    BufferedImage subbim = bim.getSubimage(170, 26, this.getWidth()-170, this.getHeight()-26);
    AffineTransform tx = new AffineTransform();
    tx.scale(2, 2);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
    subbim = op.filter(subbim, null);
    try {
    Date data = GregorianCalendar.getInstance().getTime();
    ImageIO.write(subbim, "png", new File("C:/obrazy/screan_2d_" +data.getTime()+ ".png"));
    }


    sending file:
    String destionationPath = "upload.php";
    String fileFieldName = "file";

    File f = new File("C:/obrazy/screan_2d.png");
    FileInputStream in = null;
    DataOutputStream out = null;
    HttpURLConnection con = null;
    try
    {
    in = new FileInputStream(f);
    String path = getCodeBase().toString()+destionationPath;
    URL url = new URL(path);
    con = (HttpURLConnection) url.openConnection();

    con.setUseCaches(false);
    con.setDefaultUseCaches(false);
    con.setRequestMethod("POST");
    con.setRequestProperty("Connection", "Keep-Alive");
    String boundary = "JakisSeparator";
    con.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
    con.setDoOutput(true);
    con.setDoInput(true);

    String endl = "\r\n";
    out = new DataOutputStream(con.getOutputStream());

    out.writeBytes("--"+boundary+endl);
    out.writeBytes("Content-Disposition: form-data; name=\""+fileFieldName+"\"; filename=\"" + URLEncoder.encode(f.getName()) +"\"" + endl);
    out.writeBytes("Content-Type: application/octet-stream"+endl);
    out.writeBytes(endl);

    byte[] bytes = new byte[1024];
    int read = 0;
    while((read = in.read(bytes)) != -1)
    {
    out.write(bytes, 0, read);
    }
    out.writeBytes(endl+"--"+boundary+"--"+endl);
    out.flush();
    con.connect();
    }


    and creating PDF:
    Document document = new Document();
    try {
    Date data = GregorianCalendar.getInstance().getTime();
    FileOutputStream fos1 = new FileOutputStream(new File("C:/obrazy/mydoc_" +data.getTime()+ ".pdf"));

    PdfWriter writer1 = PdfWriter.getInstance(document,fos1);
    document.open();
    Paragraph nagl = new Paragraph("Start");
    document.add(nagl);
    //(...)
    document.close();
    }


    Can someone help, how transform this code to may direct use for example DataInPutStream, DataOutputStram objects or something similar, without saving file on local disc?
    Last edited by anurit; 03-03-2010 at 01:54 PM.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    I was thinking, in theory it might be possible to use the PipedInputStream and PipedOutputStream, ?

    Java Code:
    PipedOutputStream pOut = new PipedOutputStream();
    PipedInputStream pIn = new PipedInputStream(pOut);
    where we have the image creator generate its image output to an instance of PipedOutputStream, and the sending file operation would read from its associated PipedInputStream.

    The catch with using these is we need to have the reading and writing operations done in two threads. If we currently do things successively in the main application thread, perhaps try to create a thread to do the image generation, start it up to start writing to the piped output stream, and then in the main thread start reading the piped input stream

    another option I though of, you can try to encapsulate the logic of sending the file into an object that extends java.io.OutputStream. for example

    Where the constructor takes on arguments for the remote URL, and performs any connection operations when the class is created. then passing this fancy output stream to the image writer would cause it to really do the upload as the image is being written.

    I suppose a similar principle could be employed in the generation of the PDF file, but I am not clear how or what it would render out to. an applet is already in the client side, I am not sure it would be possible to have it some how display the pdf from the applet if it was not already a file.

  3. #3
    anurit is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for your help. I try this pipe streams use. Now I use signed applet...

Similar Threads

  1. How to create file into another host from a host
    By varadha in forum Networking
    Replies: 3
    Last Post: 02-10-2010, 09:03 PM
  2. copy file from local host to remote host
    By isotoper in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 04:41 PM
  3. Need Help on Remote Host Screen Capture
    By krishnaraoveera1294 in forum AWT / Swing
    Replies: 4
    Last Post: 03-13-2009, 10:56 PM
  4. Replies: 0
    Last Post: 10-31-2008, 07:07 PM
  5. How to find file created date.....
    By roshithmca in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2008, 09:48 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
  •