Tag: 连接

ftp没有在java中正确下载文件?

当我使用以下代码下载文件时,它只是将文件写入本地目标,但文件大小都为零。 任何人都可以说为什么会发生这种情况以及如何解决它? import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import java.io.FileOutputStream; import java.io.IOException; public class FtpDownload { public static void main(String[] args) { FTPClient client = new FTPClient(); FileOutputStream fos = null; String filename = “config.zip”; try { client.connect(“ftpsrv”); client.login(“user”, “user”); for (FTPFile file : client.listFiles()) { filename = “C:\\tmp\\user\\” + file.getName(); if (file.isFile()) { fos = new […]

由于SocketException,RabbitMQ新连接被拒绝

在尝试创建与另一台服务器上运行的rabbitmq的新连接时,我收到以下错误: java.io.IOException at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106) at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102) at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124) at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:406) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:516) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:533) Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; reason: java.net.SocketException: Connection reset at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67) at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33) at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343) at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216) at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118) Caused by: java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at java.io.BufferedInputStream.fill(Unknown Source) at java.io.BufferedInputStream.read(Unknown Source) at java.io.DataInputStream.readUnsignedByte(Unknown Source) at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:95) […]

如何确定,如果互联网连接当前可用且在Android设备上有效?

我有这个Android应用程序,需要通过互联网从远程服务器加载数据。 这种更新function显然要求设备不仅要连接到某种网络,还要连接到互联网。 所以:想要将更新服务安排到某个日期,当它开始时,它应该确定它是否真的可以到达目标服务器。 因此,一个简单的“是连接到wifi的设备?” 这是不够的,因为该设备可能连接到不提供互联网接入的无线网络。 像PING这样的东西是必需的…… 最简单/最好的方法是什么,如果有互联网连接可用,即服务器可以访问?

Android设备到PC的套接字连接

我面临着从Android设备到PC的特定端口(如8080建立套接字连接的问题。 我只想创建一个套接字,它将连接到特定端口,并在该端口上写入一些数据流。 我为此目的编写了一些代码,但代码给了我一个例外: TCP Error:java.net.ConnectException:/127.0.0.1:8080-connection refused 我给出的代码如下: private static TextView txtSendStatus; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initControls(); String sentence = “TCP Test #1n”; String modifiedSentence; try { Socket clientSocket = new Socket(“192.168.18.116”, 8080); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new […]

Java JDBC效率:连接需要多长时间?

我还在努力解决同样的问题。 它似乎工作正常,特别是在创建如下所示的AbstractModel类之后: public abstract class AbstractModel { protected static Connection myConnection = SingletonConnection.instance().establishConnection(); protected static Statement stmt; protected static ResultSet rs; protected boolean loginCheck; // if userId and userLoginHistoryId are valid – true, else false protected boolean userLoggedIn; // if user is already logged in – true, else false public AbstractModel (int userId, Long userLoginHistoryId){ […]

JDBC – 用于只读操作的setAutoCommit

假设我有一个创建数据库连接的常用方法: Connection getConnection() throws SQLException { Connection con = … // create the connection con.setAutoCommit(false); return con; } 我在这里setAutoCommit(false)调用,以便此方法的调用者不必担心设置它。 但是,如果调用者执行的操作只是读取数据,这是不好的做法吗? 有没有额外的开销? 我个人认为,最好将逻辑集中在一个地方,这样调用者就不必设置自动提交,这样可以避免代码冗余。 我只是想确保它不会给只读操作带来任何不必要的开销。

“com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败”到远程数据库

我试图连接到我的远程MySQL数据库,但我失败了并得到了这个错误。 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 令人困惑的是,当我使用MySQL-Front工具连接远程数据库时,它是有效的,我可以成功ping到IP地址。 但是当我使用我的代码时,它会在大约十秒后显示错误。 此外,当我在我的代码中使用了错误的用户名或密码时,它会立即显示错误的validation。 这是否certificate设置连接是没有问题的? 这是我的代码(它可以在我的localhost数据库上工作): public static void main(String[] args) { String url = “jdbc:mysql://(IP address):3306/”; String dbName = “talk”; String driver = “com.mysql.jdbc.Driver”; String userName = “talkroot”; String password = “123456”; try { Class.forName(driver).newInstance(); Connection conn = DriverManager.getConnection(url + dbName, userName, password); System.out.println(“connect”); conn.close(); } catch (Exception e) { […]

例外,以及重置连接时如何最好重试?

我有一些代码连接到URL以下载文件,然后对其执行一些处理。 但是,有时我收到错误java.net.SocketException: Connection reset 。 我想在收到此错误时重试下载该文件,在放弃之前最多说3次。 我想知道什么是最好的结构方式。 以下是否正常。 将try-catch块放在while循环中似乎可以接受,还是有更好的方法? 非常感谢所有帮助! while(!connected && retries > 0) { retries–; URL downloadUrl; URLConnection conn; try { downloadUrl = new URL(url); conn = downloadUrl.openConnection(); conn.connect(); connected = true; // Perform processing on downloaded file here } catch (IOException e) { Logger.batchLog(e); } }

S3 Java客户端因“Content-Length delimited message body的过早结束”或“java.net.SocketException Socket closed”而失败了很多

我有一个应用程序在S3上做了很多工作,主要是从它下载文件。 我看到很多这类错误,我想知道这是否是我的代码上的内容,或者这项服务是否真的不可靠。 我用来从S3对象流中读取的代码如下: public static final void write(InputStream stream, OutputStream output) { byte[] buffer = new byte[1024]; int read = -1; try { while ((read = stream.read(buffer)) != -1) { output.write(buffer, 0, read); } stream.close(); output.flush(); output.close(); } catch (IOException e) { throw new RuntimeException(e); } } 此OutputStream是一个新的BufferedOutputStream(新的FileOutputStream(文件)) 。 我使用的是最新版本的Amazon S3 Java客户端,在放弃之前会重试此调用四次 。 因此,在尝试了4次后,它仍然失败。 关于如何改进这一点的任何提示或提示都表示赞赏。

在我想要返回ResultSet时关闭JDBC连接的位置

当我关闭Connection时,似乎会自动关闭ResultSet 。 但我想返回ResultSet并在另一个方法中使用它,然后我不知道在哪里关闭Connection和PreparedStatement 。 public ResultSet executeQuery(String sql, String[] getValue) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = getConn(); pstmt = conn.prepareStatement(sql); if (getValue != null) { for (int i = 0; i < getValue.length; i++) { pstmt.setString(i + 1, getValue[i]); } } rs = pstmt.executeQuery(); […]