服务器套接字文件传输
我在java中使用服务器套接字概念来传输图像和视频等文件。但是当我在客户端接收时,我正在自定义文件名。我可以获得文件的原始名称吗?
例如:
如果来自服务器端的用于传输的文件是“abc.txt”,我需要在客户端中反映相同的名称(无需单独传递名称)。
在服务器端:
public class FileServer {
public static void main (String [] args ) throws Exception {
// create socket
ServerSocket servsock = new ServerSocket(13267);
while (true) {
System.out.println("Waiting...");
Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
OutputStream os = sock.getOutputStream();
new FileServer().send(os);
sock.close();
}
}
public void send(OutputStream os) throws Exception{
// sendfile
File myFile = new File ("C:\\User\\Documents\\abc.png");
byte [] mybytearray = new byte [(int)myFile.length()+1];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
}
}
在客户端:
public class FileClient{
public static void main (String [] args ) throws Exception {
long start = System.currentTimeMillis();
// localhost for testing
Socket sock = new Socket("127.0.0.1",13267);
System.out.println("Connecting...");
InputStream is = sock.getInputStream();
// receive file
new FileClient().receiveFile(is);
long end = System.currentTimeMillis();
System.out.println(end-start);
sock.close();
}
public void receiveFile(InputStream is) throws Exception{
int filesize=6022386;
int bytesRead;
int current = 0;
byte [] mybytearray = new byte [filesize];
FileOutputStream fos = new FileOutputStream("def");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
bos.close();
}
}
I have used server socket concept in java to transfer files like images and videos. But when i receive at the client side, i am customizing the file names. Can i get the original name of the file as it is?
For Example:
If the file from server end for transfer is "abc.txt", i need this same name to be reflected in the client end(without passing the name separately).
In the server end:
public class FileServer {
public static void main (String [] args ) throws Exception {
// create socket
ServerSocket servsock = new ServerSocket(13267);
while (true) {
System.out.println("Waiting...");
Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
OutputStream os = sock.getOutputStream();
new FileServer().send(os);
sock.close();
}
}
public void send(OutputStream os) throws Exception{
// sendfile
File myFile = new File ("C:\\User\\Documents\\abc.png");
byte [] mybytearray = new byte [(int)myFile.length()+1];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
}
}
In the client end:
public class FileClient{
public static void main (String [] args ) throws Exception {
long start = System.currentTimeMillis();
// localhost for testing
Socket sock = new Socket("127.0.0.1",13267);
System.out.println("Connecting...");
InputStream is = sock.getInputStream();
// receive file
new FileClient().receiveFile(is);
long end = System.currentTimeMillis();
System.out.println(end-start);
sock.close();
}
public void receiveFile(InputStream is) throws Exception{
int filesize=6022386;
int bytesRead;
int current = 0;
byte [] mybytearray = new byte [filesize];
FileOutputStream fos = new FileOutputStream("def");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
bos.close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了让接收者知道文件名,
a)它必须假设它知道该名称,因为它要求它,
b)服务器首先将名称作为流的一部分发送。
如果您发明了一种无需实际发送信息即可发送信息的方法,请告诉我,我们就可以成为亿万富翁。我们可以称之为“计算机心灵感应”。
For the receiver to know the file name, either:
a) it must assume it knows the name because it asked for it,
b) the server sends the name first as part of the stream.
If you invent a way to send information without actually sending it, let me know and we can become billionaires. We can call it 'computer telepathy'.
是的,只需在实际文件内容之前传输元数据(在您的情况下是
myFile.getName()
),然后让客户端和服务器读取并发出该元数据。最好使用已建立的协议,例如 HTTP 及其Content-Disposition
标头。Yes, simply transfer the metadata (in your case
myFile.getName()
) before the actual file contents, and make client and server read and emit that metadata. It's a good idea to use established protocols, for example HTTP and itsContent-Disposition
header.请参阅此参考文献,可能会有所帮助...
http://www.rgagnon.com/javadetails /java-0542.html
http://www.java2s.com/Code/Java/Network-Protocol/TransferafileviaSocket.htm
http://www.artima.com/forums/flat.jsp?forum=1&thread=34857
see this references,May be helpful...
http://www.rgagnon.com/javadetails/java-0542.html
http://www.java2s.com/Code/Java/Network-Protocol/TransferafileviaSocket.htm
http://www.artima.com/forums/flat.jsp?forum=1&thread=34857
有一个更简单的方法可以做到这一点。只需将输入流包装在 DataInputStream 中并使用 .writeUTF(myFile.getName()) 等方法即可。同样,您可以通过在 DataInputStream 上应用 .readUTF 来读取接收器处的文件名。这是示例代码:
发件人:
收件人:
There is a simpler way to do this. Just wrap your input stream inside DataInputStream and use methods like .writeUTF(myFile.getName()). Similarly, you can read file name at receiver by applying .readUTF on DataInputStream. Here is a sample code:
Sender:
Receiver: