【发布时间】:2017-06-03 17:53:49
【问题描述】:
我收到 FileNotFound 错误。基本上,我正在尝试将文件从客户端上传到服务器。
请帮帮我。
这是 client.java 类 打包ftppackage;
import java.net.*;
import java.io.*;
public class Client {
public static void main (String [] args ) throws IOException {
Socket socket = new Socket("127.0.0.1",15123);
File transferFile = new File ("D:\\AsiaAd.wmv");
byte [] bytearray = new byte [(int)transferFile.length()];
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray,0,bytearray.length);
OutputStream os = socket.getOutputStream();
System.out.println("Sending Files...");
os.write(bytearray,0,bytearray.length);
os.flush();
socket.close();
System.out.println("File transfer complete");
}
}
这是我的 server.java 类
package ftppackage;
import java.net.*;
import java.io.*;
public class Server {
public static void main (String [] args ) throws IOException {
int filesize=1022386;
int bytesRead;
int currentTot = 0;
ServerSocket serverSocket = new ServerSocket(15123);
Socket socket = serverSocket.accept();
System.out.println("Accepted connection : " + socket);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("E:\\0\\"); // it is creating new file not copying the one from client
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(bytearray,0,bytearray.length);
currentTot = bytesRead;
do {
bytesRead = is.read(bytearray, currentTot, (bytearray.length-currentTot));
if(bytesRead >= 0)
currentTot += bytesRead;
} while(bytesRead > -1);
bos.write(bytearray, 0 , currentTot);
bos.flush();
bos.close();
socket.close();
}
}
另外,请指导我如何使用百分比在其中添加进度条。我在这里阅读了有关 SwingWorker 的信息,但由于我对线程概念完全陌生,因此无法实现它。 谢谢你考虑我的问题。
【问题讨论】:
-
FileNotFoundException的文字告诉你原因。可能是权限问题,但只有您可以知道,因为您还没有发布它。你需要看看ProgressMonitorInputStream。 -
当我添加 randomFile.ext 意味着任何随机文件然后,它被创建。如果在 E:/0 中写入存在权限问题,那么为什么会创建一些随机文件?
-
我一个字都听不懂,但您应该已经接受了提示并检查了异常文本,或者确实将其发布在此处。
-
向我们展示异常调用堆栈和确切的异常消息。
标签: java sockets ftp swingworker