java - not able to transfer Image file -
hi client program transfering image , while transfering image file getting corrupted , not able open image file , i'm not able identify bug , can 1 me.
datainputstream input = new datainputstream(s.getinputstream()); dataoutputstream output = new dataoutputstream(s.getoutputstream()); system.out.println("writing......."); fileinputstream fstream = new fileinputstream("blue hills.jpg"); datainputstream in = new datainputstream(fstream); byte[] buffer = new byte[1000]; int bytes = 0; while ((bytes = fstream.read(buffer)) != -1) { output.write(buffer, 0, bytes); } in.close();
i assume s
socket
, you're attempting transfer file on network? here's example of sending file sockets. sets server socket in thread , connects itself.
public static void main(string[] args) throws ioexception { new thread() { public void run() { try { serversocket ss = new serversocket(3434); socket socket = ss.accept(); inputstream in = socket.getinputstream(); fileoutputstream out = new fileoutputstream("out.jpg"); copy(in, out); out.close(); in.close(); } catch (ioexception e) { e.printstacktrace(); } } }.start(); socket socket = new socket("localhost", 3434); outputstream out = socket.getoutputstream(); fileinputstream in = new fileinputstream("in.jpg"); copy(in, out); out.close(); in.close(); } public static void copy(inputstream in, outputstream out) throws ioexception { byte[] buf = new byte[8192]; int len = 0; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } }
Comments
Post a Comment