• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pdf getting corrupted while transfering through FTP

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i m trying a transfer a pdf file from one server from another.
I m able to get the file through inputstream and got to write it through FileOutputStream at the desired place. the pdf file is getting created but when i try to open it , it shows me a message that this file has been corrupted. Also the size of the copied file is very large(98Kb) as compared to the original(28kb).
Could any one give a clue to the solution. Below is the code i have written.
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTP;
//import org.apache.tools.ant.taskdefs.optional.net.FTP


public class CpyFile
{

public static final String FTP_APPEND_SUCCESS = "FTP_APPEND_SUCCESS";
public static final String FTP_APPEND_DATA_TRUNCATED = "FTP_APPEND_DATA_TRUNCATED";



public static void main(String[] args)
{
FTPClient client = null;
boolean boolLogin = false;
client = new FTPClient();

try
{



int maxTries = 3 ;
int tryCount = 0;
while (!boolLogin && tryCount++ < maxTries) {
client.connect("172.19.54.55");
boolLogin = client.login("upload", "upload");
}

if (!boolLogin) {

String ipaddress = "172.19.54.55";
String userid = "upload";
String password = "upload";
System.out.println(
"Unable to Connect to FTP Server IP Asdress: " + ipaddress + " user id: " + userid + " password: " + password);


}
else
{
System.out.println("****CONNECTED********");
System.out.println(client.getReplyString());


System.out.println("@@@@"+client.printWorkingDirectory());
FTPFile files[] = client.listFiles();
int i = files.length;
int j =0;
while( j < i)
{
System.out.println("+++"+files[j].getName());
j++;
}
ystem.out.println("!!!"+client.getDataConnectionMode());
client.enterLocalPassiveMode();
System.out.println("&&&&"+client.getDataConnectionMode()+"******");
boolean bool = client.changeWorkingDirectory("\\upload");
System.out.println(client.printWorkingDirectory());
int l = 0;
FTPFile files1[] = client.listFiles();
int k = files1.length;
while( l < k)
{
System.out.println("+++"+files1[l].getName());
l++;
}
FileOutputStream fStream = new FileOutputStream ("C:\\ftproot\\3996_GD_1144406443921.pdf");
OutputStream outstream = null ;
InputStream instream = null;
client.setFileType(FTP.BINARY_FILE_TYPE,FTP.BINARY_FILE_TYPE);
client.setFileTransferMode(FTP.BINARY_FILE_TYPE);

instream = client.retrieveFileStream("\\3996_GD_1144406443921.pdf");
byte[] data = new byte[99999];
int m =0, n=0;
m = instream.read(data);
System.out.println("@@"+data[0]);
System.out.println("@@"+m);
fStream.write(data);
fStream.close();
}
}


catch (java.io.IOException e)
{
e.printStackTrace();
}
finally
{
try
{
System.out.println("#######"+client.getDataConnectionMode()+"####"+client.getSystemName());
client.disconnect();


}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, dhams. Perhaps you missed our naming policy on the way in, but we ask that everyone use a real (or at least real-sounding) first and last name. Keeps things friendly around here. You can change your displayed name here
As for your problem, Read Doesn't Do What You Think It Does.
[ April 20, 2006: Message edited by: Joe Ess ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to tell the Apache Commons-net FTP component that you're transferring a binary file instead of an ASCII file.
 
gaurav kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx for the replies...
got the issue resolved by taking help from a FileOutputStream object and then writing the data onto it.
Probably there was some problem with reading of the data through InputStream object.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[ UD: Alan, you're recommending a product available at a web site that looks a lot like your email address. So I'm assuming you work there. Yet you don't put in a disclaimer of a possible bias. I'd say that grounds enough for removing this post. Plus, this topic is two years old, and has long since been resolved without resorting to any tools. ]
[ April 08, 2008: Message edited by: Ulf Dittmer ]
reply
    Bookmark Topic Watch Topic
  • New Topic