gpt4 book ai didi

java - 如何修复上传操作上的 UnknownHostException

转载 作者:行者123 更新时间:2023-12-01 11:24:47 28 4
gpt4 key购买 nike

我有一个代码,用于在 ftp 服务器上上传一些 txt。因此,如果我尝试使用此代码,有时有效,有时无效。

我有这个错误:

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)

这是我使用的代码:

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." );
}
}

该行的错误

bos = new BufferedOutputStream( urlc.getOutputStream() );

我找不到问题。

最佳答案

如您所知,在与 FTP 的连接中,可能会发生一些问题,断开连接、信息丢失、主机无法访问、连接超时等...您永远无法确定所有问题都会按预期发生,因此,您必须检查所有问题FTP 连接期间可能发生(抛出)的问题(异常)。

正如我所提到的,FTP 连接中存在多个问题,因此在本示例中,我们将仅捕获您显示的问题所有其他异常

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 );
}
}
// catch UnknownHostException problem
catch (UnknownHostException e) {
System.out.println( "Unknown Host.");
}
// catch all other problems that can happen with ftp connection
catch (Exception e) {
// do what you want in case of other errors
}
finally
{
// finally block (same you have)
}

但是,我强烈建议您检查 FTPClient来自 ApacheCommons。 FTPClient 易于使用和理解,将使您的生活更轻松,让您的代码更加稳定和可读。

关于java - 如何修复上传操作上的 UnknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30915168/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com