java 从网络设备读取文件
2022-09-04 08:42:14
有人可以帮我找到一个教程或示例java代码
从同一网络中的任何计算机读取文件
执行此操作的最简单方法是使用常规文件路径读取它。
在视窗上:
new File("\\\\server\\path\\to\\file.txt")
// (double-backslashes required for backslashes in path)
在Unix上:
首先使用 Samba(SMB、NFS 或任何其他协议)将共享装载到某个位置,如 /mnt/network。然后,您可以使用:
new File("/mnt/network/path/to/file.txt")
拥有File对象后,您可以使用FileInputStream,FileReader或您想要读取文件的任何其他内容。
编辑评论响应。如果您使用的是小程序,则可能需要从 Web 服务器拉取文件。你可以使用内置的java.net.URL类,但如果你必须做的不仅仅是简单的事情,我会推荐这个:http://hc.apache.org/httpclient-3.x/index.html
示例(来自共享资源 HTTP 站点):
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(url);
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
}
}
这并不那么简单!要使用服务器客户端应用程序,您需要一个网络 API。
我有1个DeBukkit和一个扩展版本。如果您要发送文件,我会建议我的一个(服务器客户端扩展.jar)becazse有一个发送文件的选项(FilePacket.java)。
这是指向库的链接:所有库
服务器用于客户端的扩展服务器的代码:
public class TestServer extends Server {
public TestServer() {
super(29898, true, true,true);
registerMethod("bt", new FileReciver() {
@Override
public void onCompleteRecive(FileInfo data) {
System.out.println("Completely recived : "+data);
Path p = Paths.get(data.getName());
try {
Files.createFile(p);
Files.write(p, data.getContent());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see com.bebukkit.niton.network.packets.buffer.BufferedPacketReciver#run(com.bebukkit.niton.network.packets.Packet, java.net.Socket)
*/
@Override
public void run(Packet<? extends Serializable> msg, Socket socket) {
super.run(msg, socket);
sendMessage(new Packet<Boolean>("", null), socket);
}
});
}
@Override
public void preStart()
{
registerMethod("msg", new ReciveMethod() {
@Override
public void run(Packet<? extends Serializable> msg, Socket socket) {
broadcastMessage(msg);
}
});
}
@Override
public void onWrongDataPacketException(ClassNotFoundException e) {
// TODO Auto-generated method stub
}
@Override
public void onReceivePacketError(IOException e) {
// TODO Auto-generated method stub
}
@Override
public void onPacketReckognized(ServerSocket socket) {
// TODO Auto-generated method stub
}
@Override
public void onPacketSendException(Exception e, Packet<? extends Serializable> message, Socket socket2) {
// TODO Auto-generated method stub
}
@Override
public void onSendPacketToNotConnectedClient(Packet<? extends Serializable> message, Socket socket2) {
// TODO Auto-generated method stub
}
@Override
public void onBrodcast(Packet<? extends Serializable> pack) {
// TODO Auto-generated method stub
}
@Override
public void onServerStartError(IOException e) {
// TODO Auto-generated method stub
}
@Override
public void onServerStop() {
// TODO Auto-generated method stub
}
}
客户端代码:
package com.bebukkit.niton.network.test;
import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;
import java.util.Scanner;
import com.bebukkit.niton.network.Client;
import com.bebukkit.niton.network.packets.Packet;
import com.bebukkit.niton.network.packets.ReciveMethod;
public class TestClient extends Client {
public TestClient() {
super("localhost", 29898, 5000, false,true);
registerMethod("msg", new ReciveMethod() {
@Override
public void run(Packet<? extends Serializable> msg, Socket socket) {
System.out.println(msg.getData());
}
});
registerMethod("replay", new ReciveMethod() {
@Override
public void run(Packet<? extends Serializable> msg, Socket socket) {
System.out.println("REREplay");
}
});
}
@Override
public void onSocketClosingFail() {
// TODO Auto-generated method stub
}
@Override
public void onLoginPacketSendingFailed(IOException ex) {
// TODO Auto-generated method stub
}
@Override
public void onConnectionError(IOException ex) {
// TODO Auto-generated method stub
}
@Override
public void onMessageReciveError(Exception ex) {
// TODO Auto-generated method stub
}
@Override
public void onPacketSendError(Exception ex, Packet<? extends Serializable> pack) {
// TODO Auto-generated method stub
}
@Override
public void start() {
super.start();
}
}
您需要一个单独的服务器 + 客户端启动器:
package com.bebukkit.niton.network.test;
public class ServerStarter {
public static void main(String[] args) {
new TestServer();
}
}
客户:
package com.bebukkit.niton.network.test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.swing.JFileChooser;
import com.bebukkit.niton.network.packets.Packet;
import com.bebukkit.niton.network.packets.buffer.BufferedPacket;
import com.bebukkit.niton.network.packets.file.FileInfo;
import com.bebukkit.niton.network.packets.file.FilePacket;
public class ClientStarter {
public static void main(String[] args) throws IOException {
TestClient tc = new TestClient();
tc.start();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JFileChooser c = new JFileChooser("Choose a file to upload");
c.setFileSelectionMode(JFileChooser.FILES_ONLY);
c.showDialog(null,"Upload");
File f = c.getSelectedFile();
try {
tc.sendMessage(new FilePacket("file", f));
tc.sendMessage(new Packet<String>("replay","test"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}