Solved

Cpying PDF file from distant server with java

Posted on 2004-04-27
3
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 45
How do I remove an object from a 3 60
ejb mdb examples 1 36
Java List 4 73
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses

737 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