java socket file transfer output path -
i'm working on java project transfers files between server , client , i've managed send file desired output location, problem have include full file name in output path save successfully. program runs in way:
first gets path of file transferred input console, , gets output path, again input console.
here codes of corresponding file name import , exports(i think problem somewhere here , posting part sufficed)
server side
.... string in_filepath = null; system.out.print("enter file name: "); in_filepath = sc.nextline(); file myfile = new file( in_filepath ); system.out.println("the file chosen being sent..."); byte[] mybytearray = new byte[(int) myfile.length()]; fileinputstream fis = null; try { fis = new fileinputstream(myfile); sc.close(); } catch (filenotfoundexception ex) { ex.printstacktrace(); }
client side
..... int buffersize = clientsocket.getreceivebuffersize(); = clientsocket.getinputstream(); datainputstream clientdata = new datainputstream(is); string filename = clientdata.readutf(); system.out.println("file transferred is: " + filename ); system.out.print("file output path: "); string out_filepath; out_filepath = sc.nextline(); file file = new file( out_filepath ); fos = new fileoutputstream( file ); bos = new bufferedoutputstream(fos); bytesread = is.read(abyte, 0, abyte.length); { baos.write(abyte); bytesread = is.read(abyte); } while (bytesread != -1); bos.write(baos.tobytearray()); system.out.println(filename + " transferred successfully");
at first haven't included output path in program; expected output path root project folder , working great reading filename , sending file same name without problems. when implemented output path query, output paths choose "c:\" or "c:\blabla\" gives me exception stated above. giving output path "c:\image.jpg" or "blablabla\image.jpg" works well(assuming name of file copied image.jpg) can problem reading file name? appreciated
edit: i'm receiving socket write error if had given "c:\" (or kind of paths that) output path, yet still works if output path given "c:\image.jpg"
assume you're executing program on directory "c:\", , file "image.jpg" located on "c:\images\image.jpg". if provide input "image.jpg", won't work. you'd have provide "images\image.jpg" instead.
you can check if you've hit actual file adding following line server side code:
system.out.println(myfile.isfile());
Comments
Post a Comment