Solved

Cpying PDF file from distant server with java

Posted on 2004-04-27
3
221 Views
Last Modified: 2010-03-31
How to write a java program which copy a PDF file from a distant server to ower machine, this portion of program do this but the PDF file contain an error while reading by AdobAcrobeat.
try
 {
   URL urlFichierHello = new URL("http://www.loria.fr/projets/TALN/actes/Recital/posters/nouali.pdf");
   URLConnection connexion   = urlFichierHello.openConnection ();
   InputStream   fluxFichier = connexion.getInputStream ();
   byte contenuFichier [ ] = new byte [connexion.getContentLength ()];
   int  octetsLus = fluxFichier.read (contenuFichier);
   fluxFichier.close ();
   try
     {
       RandomAccessFile  file_to_ponc = new RandomAccessFile("C:\\1.pdf","rw");
       file_to_ponc.write(contenuFichier);
       file_to_ponc.close();
      }
      catch(java.io.IOException e){e.printStackTrace();}  
 }
 catch (Exception e){e.printStackTrace();}
thanks
0
Comment
Question by:zs_said
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 10927852
I would alter your program a little. There's no need to use a RAF:


InputStream   fluxFichier = connexion.getInputStream ();
FieOutputStream out = new FileOutputStream(new File("c:/1.pdf"));
final int BUF_SIZE = 8192;
byte contenuFichier [ ] = new byte [BUF_SIZE];
int bytesRead = -1;
while ((bytesRead = fluxFichier.read(contenuFichier)) > -1) {
    out.write(contenuFichier, 0, bytesRead);
}
fluxFichier.close();
out.close();

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 52
login form jsp example 2 37
Why doesn't this text field show up on my Applet frame? 2 19
Java 8 to Java 6 8 20
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

861 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