You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Ari Fainchtein <ar...@lechido.com> on 2012/12/07 15:16:55 UTC

Binary Files get corrupted with FTPClient

Hello,

I am trying to use the FTPClient to upload files and everything is working fine except that the files arrive all garbled to the server.  I have tried with pdfs and jpegs and  in both case the files are corrupted.

Any help would be appreciated.

  Here is the code I am using:
<br>

<code>
 try {
				
				
		    	
		    	String server = "XX.XX.XX.XX";;
				FTPClient ftpClient = new FTPClient();
				  
				ftpClient.connect(server);
				ftpClient.login("abc", "abc");
				
			    
			    
			    boolean error = false;
			    try {
			      int reply;
			      

			      
			      reply = ftpClient.getReplyCode();
			      
			      if(!FTPReply.isPositiveCompletion(reply)) {
			    	  ftpClient.disconnect();
			        System.err.println("FTP server refused connection.");
			        JOptionPane.showConfirmDialog(SendFileMonitorDialog.this, "Server refused connection", "Error",JOptionPane.OK_OPTION);
			      }
			      ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
				  ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
			      
			      
                  FileInputStream fileInputStream = new FileInputStream(selectedFile);
                  BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
                  ProgressMonitorInputStream progressMonitorInputStream = new ProgressMonitorInputStream(SendFileMonitorDialog.this, "Uploading " + selectedFile.getName(), bufferedInputStream);
                  
                  
                  ftpClient.makeDirectory( publicFolderName);
                  ftpClient.changeWorkingDirectory(publicFolderName);
                  
                  
                  ftpClient.storeFile(selectedFile.getName(), progressMonitorInputStream);
                  try {
					SwingUtilities.invokeAndWait( runner );
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                  progressMonitorInputStream.close(); 
			      ftpClient.logout();
			    } catch(IOException e) {
			      error = true;
			      e.printStackTrace();
			    } finally {
			      if(ftpClient.isConnected()) {
			        try {
			          ftpClient.disconnect();
			        } catch(IOException ioe) {
			          // do nothing
			        }
			      }
			      
			    }
			
				
	        }catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
</code>

Re: Binary Files get corrupted with FTPClient

Posted by sebb <se...@gmail.com>.
On 7 December 2012 18:20, Gary Gregory <ga...@gmail.com> wrote:
> On Fri, Dec 7, 2012 at 1:19 PM, sebb <se...@gmail.com> wrote:
>
>> On 7 December 2012 15:49, Ari Fainchtein <ar...@lechido.com> wrote:
>> > Thanks for the answer.  I actually just found the problem.  I removed
>> the line
>> >
>> > ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
>> >
>> > and that made it work. It was wrong, since the parameter was not correct.
>>
>> Actually, this line is wrong too:
>>
>> ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
>>
>> The second parameter should either be omitted, or it should be one of
>> the _FORMAT constants.
>>
>> Unfortunately the parameters are ints rather than enums so the
>> compiler cannot generate an error if you use the wrong one.
>>
>
> How about adding enums and deprecating this error prone API for the next
> release?

Not sure it's possible without breaking binary compatibility.
And there is some overlap between the modes.
However this is a discussion for the developer list, at least initially.

> Gary
>
>
>> >
>> > On 08/12/2012, at 2:42 AM, sebb wrote:
>> >
>> >> On 7 December 2012 14:16, Ari Fainchtein <ar...@lechido.com> wrote:
>> >>> Hello,
>> >>>
>> >>> I am trying to use the FTPClient to upload files and everything is
>> working fine except that the files arrive all garbled to the server.  I
>> have tried with pdfs and jpegs and  in both case the files are corrupted.
>> >>>
>> >>> Any help would be appreciated.
>> >>>
>> >>
>> >> Try using the FTP client example [1] to upload the file.
>> >> If that works, see how it differs from your code.
>> >>
>> >> [1] http://commons.apache.org/net/examples/ftp/FTPClientExample.java
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> >> For additional commands, e-mail: user-help@commons.apache.org
>> >>
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
>
>
> --
> E-Mail: garydgregory@gmail.com | ggregory@apache.org
> JUnit in Action, 2nd Ed: <http://goog_1249600977>http://bit.ly/ECvg0
> Spring Batch in Action: <http://s.apache.org/HOq>http://bit.ly/bqpbCK
> Blog: http://garygregory.wordpress.com
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: Binary Files get corrupted with FTPClient

Posted by Gary Gregory <ga...@gmail.com>.
On Fri, Dec 7, 2012 at 1:19 PM, sebb <se...@gmail.com> wrote:

> On 7 December 2012 15:49, Ari Fainchtein <ar...@lechido.com> wrote:
> > Thanks for the answer.  I actually just found the problem.  I removed
> the line
> >
> > ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
> >
> > and that made it work. It was wrong, since the parameter was not correct.
>
> Actually, this line is wrong too:
>
> ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
>
> The second parameter should either be omitted, or it should be one of
> the _FORMAT constants.
>
> Unfortunately the parameters are ints rather than enums so the
> compiler cannot generate an error if you use the wrong one.
>

How about adding enums and deprecating this error prone API for the next
release?

Gary


> >
> > On 08/12/2012, at 2:42 AM, sebb wrote:
> >
> >> On 7 December 2012 14:16, Ari Fainchtein <ar...@lechido.com> wrote:
> >>> Hello,
> >>>
> >>> I am trying to use the FTPClient to upload files and everything is
> working fine except that the files arrive all garbled to the server.  I
> have tried with pdfs and jpegs and  in both case the files are corrupted.
> >>>
> >>> Any help would be appreciated.
> >>>
> >>
> >> Try using the FTP client example [1] to upload the file.
> >> If that works, see how it differs from your code.
> >>
> >> [1] http://commons.apache.org/net/examples/ftp/FTPClientExample.java
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> >> For additional commands, e-mail: user-help@commons.apache.org
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>


-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
JUnit in Action, 2nd Ed: <http://goog_1249600977>http://bit.ly/ECvg0
Spring Batch in Action: <http://s.apache.org/HOq>http://bit.ly/bqpbCK
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Re: Binary Files get corrupted with FTPClient

Posted by sebb <se...@gmail.com>.
On 7 December 2012 15:49, Ari Fainchtein <ar...@lechido.com> wrote:
> Thanks for the answer.  I actually just found the problem.  I removed the line
>
> ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
>
> and that made it work. It was wrong, since the parameter was not correct.

Actually, this line is wrong too:

ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);

The second parameter should either be omitted, or it should be one of
the _FORMAT constants.

Unfortunately the parameters are ints rather than enums so the
compiler cannot generate an error if you use the wrong one.

>
> On 08/12/2012, at 2:42 AM, sebb wrote:
>
>> On 7 December 2012 14:16, Ari Fainchtein <ar...@lechido.com> wrote:
>>> Hello,
>>>
>>> I am trying to use the FTPClient to upload files and everything is working fine except that the files arrive all garbled to the server.  I have tried with pdfs and jpegs and  in both case the files are corrupted.
>>>
>>> Any help would be appreciated.
>>>
>>
>> Try using the FTP client example [1] to upload the file.
>> If that works, see how it differs from your code.
>>
>> [1] http://commons.apache.org/net/examples/ftp/FTPClientExample.java
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: Binary Files get corrupted with FTPClient

Posted by Ari Fainchtein <ar...@lechido.com>.
Thanks for the answer.  I actually just found the problem.  I removed the line

ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);

and that made it work. It was wrong, since the parameter was not correct.


On 08/12/2012, at 2:42 AM, sebb wrote:

> On 7 December 2012 14:16, Ari Fainchtein <ar...@lechido.com> wrote:
>> Hello,
>> 
>> I am trying to use the FTPClient to upload files and everything is working fine except that the files arrive all garbled to the server.  I have tried with pdfs and jpegs and  in both case the files are corrupted.
>> 
>> Any help would be appreciated.
>> 
> 
> Try using the FTP client example [1] to upload the file.
> If that works, see how it differs from your code.
> 
> [1] http://commons.apache.org/net/examples/ftp/FTPClientExample.java
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 


Re: Binary Files get corrupted with FTPClient

Posted by sebb <se...@gmail.com>.
On 7 December 2012 14:16, Ari Fainchtein <ar...@lechido.com> wrote:
> Hello,
>
> I am trying to use the FTPClient to upload files and everything is working fine except that the files arrive all garbled to the server.  I have tried with pdfs and jpegs and  in both case the files are corrupted.
>
> Any help would be appreciated.
>

Try using the FTP client example [1] to upload the file.
If that works, see how it differs from your code.

[1] http://commons.apache.org/net/examples/ftp/FTPClientExample.java

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org