所以我有一个学校的作业,其中客户端必须发送无限制的文件到服务器。我这样做了我的想法。发送无限的文件从客户端到服务器
- 客户端将无限制的文件发送到服务器。
- 服务器将通过客户端上传的文件重定向到我的WAMP的服务器文件夹
我已经工作的代码,但是这个代码只能从客户端发送1个文件服务器。我还希望上传文件的文件名增加,因为在我的代码中它只使用1个文件名,所以如果更多客户端上传文件,它们将覆盖旧文件名。
例如:file01.rar接下来上传file02.rar等等。
这里是我的服务器代码:
public void run() {
filePath = "C:/wamp/www/file.rar";
byte[] aByte = new byte[1];
int bytesRead;
InputStream is = null;
try {
is = clientSocket.getInputStream();
} catch (IOException ie) {
ie.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (is != null) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(filePath);
bos = new BufferedOutputStream(fos);
bytesRead = is.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesRead = is.read(aByte);
} while (bytesRead != -1);
bos.write(baos.toByteArray());
bos.flush();
bos.close();
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
这里是我的客户端代码:
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(s.getOutputStream());
} catch (IOException ie) {
ie.printStackTrace();
}
if (bos != null) {
File uploadFile = new File(clientFacultyUploadTextField.getText());
byte[] myFileSize = new byte[(int)uploadFile.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(uploadFile);
} catch (FileNotFoundException fife) {
fife.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(myFileSize, 0, myFileSize.length);
bos.write(myFileSize, 0, myFileSize.length);
bos.flush();
bos.close();
clientFacultyUploadTextField.setText("Upload complete...");
} catch (IOException ie) {
ie.printStackTrace();
}
}
关于SO的第一个问题和这么好的格式化,+1 :) – Abubakkar