04. april 2004 - 22:20Der er
31 kommentarer og 1 løsning
Send en fil over netværks socket
Jeg skal bruge et komplet eksempel.
jeg har en client og en server som er forbundet via sockets.. De snakker med hinanden via bufferedreader og printwriter. Hvordan sender jeg en fil fra serveren til klienten?
Og det skal kunne være alle slags fil formater den skal kunne sende f.eks. ".swf" ".rar" ".pdf" ".png".
En fil fra serveren skal overføres til klienten, via sockets.
public class Server { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(9999); Socket s = ss.accept(); InputStream is = s.getInputStream(); OutputStream os = new FileOutputStream("C:\\z2.zip"); byte[] b = new byte[10000]; int n; while((n = is.read(b)) >= 0) { os.write(b, 0, n); } os.close(); is.close(); } }
public class Client { public static void main(String[] args) throws Exception { Socket s = new Socket("localhost", 9999); OutputStream os = s.getOutputStream(); InputStream is = new FileInputStream("C:\\z1.zip"); byte[] b = new byte[10000]; int n; while((n = is.read(b)) >= 0) { os.write(b, 0, n); } os.close(); is.close(); } }
Her lukker jeg socket når upload er færdig. Skal socket genbruges er man nødt til at sende en længde først så modtager ved hvornår alle bytes er nået frem.
DataInputStream og DataOutputStream kan være gode til det.
public class Server2 { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(9999); Socket s = ss.accept(); DataInputStream is = new DataInputStream(s.getInputStream()); save("C:\\z1copy.zip", is); save("C:\\z2copy.zip", is); save("C:\\z3copy.zip", is); is.close(); } public static void save(String filename, DataInputStream is) throws Exception { int totsize = is.readInt(); OutputStream os = new FileOutputStream(filename); byte[] b = new byte[10000]; int n; int size = 0; while(size < totsize) { n = is.read(b); os.write(b, 0, n); size += n; } os.close(); } }
public class Server2 { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(9999); Socket s = ss.accept(); DataOutputStream os = new DataOutputStream(s.getOutputStream()); download("C:\\z1.zip", os); download("C:\\z2.zip", os); download("C:\\z3.zip", os); os.close(); } public static void download(String filename, DataOutputStream os) throws Exception { int size = (int)(new File(filename)).length(); os.writeInt(size); InputStream is = new FileInputStream(filename); byte[] b = new byte[10000]; int n; while((n = is.read(b)) >= 0) { os.write(b, 0, n); } is.close(); } }
import java.io.*; import java.net.*;
public class Client2 { public static void main(String[] args) throws Exception { Socket s = new Socket("localhost", 9999); DataInputStream is = new DataInputStream(s.getInputStream()); save("C:\\z1copy.zip", is); save("C:\\z2copy.zip", is); save("C:\\z3copy.zip", is); is.close(); } public static void save(String filename, DataInputStream is) throws Exception { int totsize = is.readInt(); OutputStream os = new FileOutputStream(filename); byte[] b = new byte[10000]; int n; int size = 0; while(size < totsize) { n = is.read(b); os.write(b, 0, n); size += n; } os.close(); } }
wow Exception in thread "main" java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92) at java.net.SocketOutputStream.write(SocketOutputStream.java:136) at java.io.DataOutputStream.write(DataOutputStream.java:85) at Server2.download(Server2.java:21) at Server2.main(Server2.java:11) Tryk på en vilkårlig tast for at fortsætte . . .
har ikke lavet nogen ændringer i koden.. prøver lige det med 1000 når jeg kommer hjem iaften..
men det super simple eksempel som du har lavet (det første), det kan jeg vel sagtens bruge sammen med bufferedreader og printwriter, hvis bare jeg laver en ekstra socket??
public class Server3 { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(9999); Socket s = ss.accept(); DataOutputStream os = new DataOutputStream(s.getOutputStream()); download("C:\\z1.zip", os); download("C:\\z2.zip", os); download("C:\\z3.zip", os); os.close(); } public static void download(String filename, DataOutputStream os) throws Exception { int size = (int)(new File(filename)).length(); os.writeInt(size); InputStream is = new FileInputStream(filename); byte[] b = new byte[10000]; int n; while((n = is.read(b)) >= 0) { os.write(b, 0, n); } is.close(); } }
public class Client3 { public static void main(String[] args) throws Exception { Socket s = new Socket("localhost", 9999); DataInputStream is = new DataInputStream(s.getInputStream()); save("C:\\z1copy.zip", is); save("C:\\z2copy.zip", is); save("C:\\z3copy.zip", is); is.close(); } public static void save(String filename, DataInputStream is) throws Exception { int totsize = is.readInt(); OutputStream os = new FileOutputStream(filename); byte[] b = new byte[10000]; int n; int size = 0; while(size < totsize) { n = is.read(b, 0, Math.min(b.length, totsize - size)); // <-------- os.write(b, 0, n); size += n; } os.close(); } }
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.