java从networking设备读取文件
有人可以帮我find一个教程或示例的Java代码
从同一networking的任何机器读取文件
最简单的方法是使用常规文件path来读取它。
在Windows上:
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或其他你想要读取的文件。
编辑评论回复。 如果您正在使用Applet,则可能需要从Web服务器上获取该文件。 你可以使用内置的java.net.URL类,但是如果你不得不做更简单的东西,我会推荐这个: http : //hc.apache.org/httpclient-3.x/index.html
示例(来自Commons 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(); } } }
这不是那么简单! 要使用服务器客户端应用程序,您需要一个networkingAPI。
我有DeBukkit 1和一个扩展版本。 如果你想发送文件,我会build议我的一个(服务器客户端扩展.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(); } }
您需要一个独立的Server + Client Starter:
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(); } }
尝试以下url的教程http://www.roseindia.net/java/beginners/construct_file_name_path.shtml
我认为最好的方法是使用java.net.URL
来打开一个InputSteam,因为你可以把它推广到不一定在同一个networking上的文件。