problems reading 2 files on server
I have the code written to upload a file to the server. I want to be able to upload 2 files to the server with the same socket connection. Any ideas how to do this? Also when i upload the file it doesn't always send the whole file. Any ideas why this is happening and how i can fix this? I'll post the code I have below.
import java.io.IOException;
import java.net.ServerSocket;
publicclass Server{
/**
* @param args
* @throws IOException
*/
publicstaticvoid main(String[] args)throws IOException{
// TODO Auto-generated method stub
ServerSocket serverSocket =null;
boolean listening =true;
try{
serverSocket =new ServerSocket(12345);
}catch (IOException e){
System.err.println("Could not listen on port: 12345.");
System.exit(-1);
}
while (listening)
new ServerThreads(serverSocket.accept()).start();
serverSocket.close();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
publicclass ServerThreadsextends Thread{
private Socket socket =null;
ServerUtil archUtil =new ServerUtil();
public ServerThreads(Socket socket){
// TODO Auto-generated constructor stub
this.socket = socket;
}
publicvoid run(){
try{
BufferedReader rd =new BufferedReader(new InputStreamReader(socket.getInputStream()));
if(rd.readLine().equals("file")){
archUtil.generateTmpDir();
archUtil.uploadFile(socket,rd);
}
else
System.out.println("this is some crap");
}
catch(IOException e){
System.out.println("is this you that's given me a problem");
}
}
}
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 created **/
String RANDOM_OFFSET =new String ();
RANDOM_OFFSET =time + d.hashCode() + RANDOM_OFFSET.hashCode()+ off;
setTmpDir(RANDOM_OFFSET);
boolean success = (new File("tmp/" + RANDOM_OFFSET)).mkdir();
if (!success){
// Directory creation failed
}
/**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());
BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(tmp/" + 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;
}
}
publicclass Network{
publicstaticfinalint BUFFER_SIZE = 1024 * 50;
privatebyte[] buffer;
public Network(){
// TODO Auto-generated constructor stub
buffer =newbyte[BUFFER_SIZE];
}
publicvoid sendFile(String fileLocation, String filename)throws Exception{
Socket socket =new Socket("localhost", 12345);
PrintWriter pout =new PrintWriter(socket.getOutputStream(),true);
pout.println("file");
pout.println(filename);
BufferedInputStream in =
new BufferedInputStream(
new FileInputStream(fileLocation));
BufferedOutputStream out =
new BufferedOutputStream(socket.getOutputStream());
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!");
}
/**
* @param args
* @throws Exception
*
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Network nw = new Network();
nw.startClient();
}
*/
}

