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();

}

*/

}

[11415 byte] By [Mjacobsa] at [2007-11-27 8:20:59]
# 1
You are sending data.You need to send a message.A message contains data and other information. Like how long the data is, and how many data blocks (data which represent files) are in the message.
jschella at 2007-7-12 20:09:24 > top of Java-index,Core,Core APIs...
# 2
Could you please show me how I can achieve this..It would be greatly appreciated..
Mjacobsa at 2007-7-12 20:09:24 > top of Java-index,Core,Core APIs...
# 3

// Assuming there is an array of File[] to send:

DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

for (File file : files)

{

out.writeUTF(file.getName());

out.writeLong(file.length());

// then your copy loop, then

in.close();

}

out.close();

Write the exactly corresponding input code on the other side, adjusting the copy loop to stop when it reaches the file length transmitted before the file.

ejpa at 2007-7-12 20:09:24 > top of Java-index,Core,Core APIs...
# 4

Ok this is the code that I wrote in those sections and I am getting an error on the server...can you tell me what I am doing wrong. I just want to copy over 2 files and give them the same names.

Client: Network.java( up top)

public static final int BUFFER_SIZE = 1024 * 50;

private byte[] buffer;

public Network() {

// TODO Auto-generated constructor stub

buffer = new byte[BUFFER_SIZE];

}

public void sendFile(File f1, File f2) throws Exception {

Socket socket = new Socket("localhost", 12345);

PrintWriter pout = new PrintWriter(socket.getOutputStream(), true);

File [] files = {f1,f2};

DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

BufferedInputStream in = null;

pout.println("file");

for(int i = 0; i < files.length; i++){

out.writeUTF(files[i].getName());

out.writeLong(files[i].length());

in = new BufferedInputStream(new FileInputStream(files[i]));

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!");

}

Server: ServerUtil.java (up top)

public void uploadFile(Socket socket, BufferedReader rd){

buffer = new byte[BUFFER_SIZE];

System.out.println("accepted Socket");

try {

BufferedOutputStream out = null;

DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

for(int i = 0; i<2; i++){

setFileName(in.readUTF());

System.out.println(getFileName());

out = new BufferedOutputStream(new FileOutputStream(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();

}

}

I am getting the error:

java.io.UTFDataFormatException

at java.io.DataInputStream.readUTF(DataInputStream.java:713)

at EpkgArchiveServerUtil.uploadFile(EpkgArchiveServerUtil.java:100)

at ServerThreads.run(ServerThreads.java:38)

Mjacobsa at 2007-7-12 20:09:24 > top of Java-index,Core,Core APIs...
# 5

I think you need to read what I wrote again.

Your input code isn't 'exactly corresponding' to your output code at all; you're writing "file" with a PrintWriter, after I said not to use a PrintWriter at all; you're writing the file length but not reading it; and you're not using that file length to control how much data you read for that file.

ejpa at 2007-7-12 20:09:24 > top of Java-index,Core,Core APIs...