Solved

Cpying PDF file from distant server with java

Posted on 2004-04-27
3
208 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now