I have a code that I use to upload some txt on ftp server. So if I try to use this code sometimes works and sometimes not works.
I have this error:
2015-06-17 20:43:33 DEBUG [LoggerFactory.MyLog4J:42] - java.net.UnknownHostException: easyeuc.altervista.org
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.ftp.impl.FtpClient.doConnect(Unknown Source)
at sun.net.ftp.impl.FtpClient.tryConnect(Unknown Source)
at sun.net.ftp.impl.FtpClient.connect(Unknown Source)
at sun.net.ftp.impl.FtpClient.connect(Unknown Source)
at sun.net.www.protocol.ftp.FtpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.ftp.FtpURLConnection.getOutputStream(Unknown Source)
at prove.FileUpload.upload(FileUpload.java:86)
at Backup.PanelChiusuraGiornata$1.doInBackground(PanelChiusuraGiornata.java:393)
at Backup.PanelChiusuraGiornata$1.doInBackground(PanelChiusuraGiornata.java:1)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
This is the code that I use:
public void upload( String ftpServer, String user, String password,
String fileName, File source ) throws MalformedURLException,
IOException{
MyLog4J log = getMyLog4j();
if (ftpServer != null && fileName != null && source != null)
{
StringBuffer sb = new StringBuffer( "ftp://" );
// check for authentication else assume its anonymous access.
if (user != null && password != null)
{
sb.append( user );
sb.append( ':' );
sb.append( password );
sb.append( '@' );
}
sb.append( ftpServer );
sb.append( '/' );
sb.append( fileName );
/*
* type ==> a=ASCII mode, i=image (binary) mode, d= file directory
* listing
*/
sb.append( ";type=i" );
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
URL url = new URL( sb.toString() );
URLConnection urlc = url.openConnection();
bos = new BufferedOutputStream( urlc.getOutputStream() );
bis = new BufferedInputStream( new FileInputStream( source ) );
int i;
// read byte by byte until end of stream
while ((i = bis.read()) != -1)
{
bos.write( i );
}
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
log.logStackTrace(ioe);
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
log.logStackTrace(ioe);
}
}
}
else
{
System.out.println( "Input not available." );
}
}
The error in on this row
bos = new BufferedOutputStream( urlc.getOutputStream() );
I'm not able to find the problem.