• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Reading and writing an Excel file from one place to another

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created a socket and am attempting to pull an excel file from an ftp website. I have a handle on the file and read it in using BufferedReader(new InputStreamReader(client.getInputStream());, When I write the file out to my local drive the file gets butchered and I can not open it! Does anyone have any ideas? I am compiling with 1.3.1. I have attempted a pdf file also and get the same results. I know how to read and write text files, that's a piece of cake. Thank you in advance for any help.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds as if you are reading the file in as binary and then writing it out as text. how are you writing this out? Please display that part of your code.
 
Sam Moran
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried 3 different ways of trying to do this and this is my latest. Each time I get the same results.
FileOutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024 * 8];
int bytesRead;
while (-1 != (bytesRead = in.read(buffer, 0, buffer.length))) {
out.write(buffer, 0, bytesRead);
}
Thank you very much for responding!
 
Tom Ben
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your entire program so I can see it please. One way or another will figure this out.
 
Sam Moran
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This program started out as a learning exercise on Sockets, but a co-worker asked if I could help her with a program that would copy a file from a secure ftp site down to our local server. The file is an Excel file. I have done testing with the follwing code on a pdf, xls, and a gif. I get the same results, a butchered file. The pdf file was the best test because I could see the results easily in my Text Editor. Extra newlines appear.
Thank you again for looking at this, sorry about the code past I am not sure what to expect when I post this!
public class ProxyTest {
public static void main( String argv[] ) {
Socket mySocket = null;
PrintWriter cmdout = null;
String theurl = "myftpsite";
String fileName = "test.xls";
String dirName = "tmp/";
String file = "d:\\my.xls";
try {
mySocket = new Socket(theurl, 21);
cmdout = new PrintWriter(new OutputStreamWriter(mySocket.getOutputStream()));
String tmp = mySocket.getInetAddress().toString();
if (mySocket == null)
throw new IOException ("The Socket never opened");
System.out.println("Address : " + tmp);
URL url = new URL("myftpsite");
URLConnection connection = url.openConnection();
cmdout.println("USER myusername");
cmdout.flush();
cmdout.println("PASS mypassword");
cmdout.flush();
cmdout.println("CWD " + dirName);
cmdout.flush();
ServerSocket server = new ServerSocket(0);
String addr = InetAddress.getLocalHost().getHostAddress();
addr = addr.replace('.', ',');
int port = server.getLocalPort();
int portHi = port / 256;
int portLo = port % 256;
addr += "," + portHi + "," + portLo;
cmdout.println("PORT " + addr);
cmdout.flush();
cmdout.println("RETR " + fileName);
cmdout.flush();
Socket client = server.accept();
BufferedInputStream in = new BufferedInputStream(client.getInputStream());
FileOutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024 * 8];
int bytesRead;
while (-1 != (bytesRead = in.read(buffer, 0, buffer.length))) {
out.write(buffer, 0, bytesRead);
System.out.println("byte : " + bytesRead);
}
in.close();
out.close();
client.close();
server.close();
} catch (UnknownHostException e) {
System.err.println("Don't know about host: myweb.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: ftp server.");
System.exit(1);
}
try {
mySocket.close();
} catch (IOException e) {
System.err.println("Can not close");
System.exit(1);
}
}
}
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic