File not found after socket transfer
I have code written to upload a file to the server. the file uploads fine when i dont specify the dir. It uploads to the server current dir, but when i try to upload it to directory "tmp/uniqueDir/filename" that i create in my code i get filnotfound exception. can someone please help me figure out why?
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
*
*/
/**
*
*
*/
publicclass ServerUtil{
String tmpDir;
String fileName;
Process proc;
Runtime runtime = Runtime.getRuntime();
publicstaticfinalint BUFFER_SIZE = 1024 * 50;
privatebyte[] buffer;
/**
*
*/
public ServerUtil(){
// TODO Auto-generated constructor stub
}
publicvoid generateTmpDir(){
java.util.Date d =new java.util.Date();
long off = (long) Math.random();
GregorianCalendar todaysDate=new GregorianCalendar();
int hour,mins,secs;
hour=todaysDate.get(Calendar.HOUR);
mins = todaysDate.get(Calendar.MINUTE);
secs = todaysDate.get(Calendar.SECOND);
String SECS = java.lang.Integer.toString(secs);
String HOUR = java.lang.Integer.toString(hour);
String MINS = java.lang.Integer.toString(mins);
String time = HOUR + MINS + SECS;
/** Unique String Name **/
String RANDOM_OFFSET =new String ();
RANDOM_OFFSET =time + d.hashCode() + RANDOM_OFFSET.hashCode()+ off;
setTmpDir(RANDOM_OFFSET);
try{
proc = runtime.exec("mkdir tmp/" + getTmpDir() );
}catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
publicvoid uploadFile(Socket socket, BufferedReader rd){
buffer =newbyte[BUFFER_SIZE];
System.out.println("accepted Socket");
try{
BufferedInputStream in =
new BufferedInputStream(socket.getInputStream());
setFileName(rd.readLine());
System.out.println(fileName);
BufferedOutputStream out =
new BufferedOutputStream(new FileOutputStream("tmp/" + getTmpDir() +"/" + getFileName()));
int len = 0;
while ((len = in.read(buffer)) > 0){
out.write(buffer, 0, len);
System.out.print("#");
}
in.close();
out.flush();
out.close();
socket.close();
System.out.println("\nDone!");
}
catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @return the tmpDir
*/
public String getTmpDir(){
return tmpDir;
}
/**
* @param tmpDir the tmpDir to set
*/
publicvoid setTmpDir(String tmpDir){
this.tmpDir = tmpDir;
}
/**
* @return the fileName
*/
public String getFileName(){
return fileName;
}
/**
* @param fileName the fileName to set
*/
publicvoid setFileName(String fileName){
this.fileName = fileName;
}
}
this is the error im getting:
java.io.FileNotFoundException: tmp/0859124333356700/fileName (A file or directory in the path name does not exist.)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:201)
at java.io.FileOutputStream.<init>(FileOutputStream.java:153)
at EpkgArchiveServerUtil.uploadFile(ServerUtil.java:93)
at ServerThreads.run(ServerThreads.java:38)

