Hi,
I am trying to create a HTTP file upload setup for a java application. So I have got a File Upload server which is working and a Client (I am not sure what changes I should do to make my Client code work with the server code) NEED HELP there. I am a java newbie so plz be kind.
import java.net.*;
import java.io.*;
public class MyClient
{
public static void main(String [] args)
{
String serverName = args[0];
int port = Integer.parseInt(args[1]);
try
{
System.out.println("Connecting to " + serverName
+ " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out =
new DataOutputStream(outToServer);
out.writeUTF("Hello from "
+ client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in =
new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
My Server code looks like this:
import java.io.*;
import java.net.*;
import java.util.*;
public class HTTPFileUploadServer extends Thread {
static final String HTML_START =
"<html>" +
"<title>HTTP POST Server in java</title>" +
"<body>";
static final String HTML_END =
"</body>" +
"</html>";
Socket connectedClient = null;
DataInputStream inFromClient = null;
DataOutputStream outToClient = null;
public HTTPFileUploadServer(Socket client) {
connectedClient = client;
}
void closeStreams() throws Exception {
inFromClient.close();
outToClient.close();
connectedClient.close();
}
...............................
// Read from InputStream
public String readLine() throws Exception {
String line = "";
char c = (char) inFromClient.read();
while (c != '\n'){
line = line + Character.toString(c);
c = (char) (inFromClient.read());
}
return line.substring(0,line.lastIndexOf('\r'));
}
public void run() {
String currentLine = null, postBoundary = null,
contentength = null, filename = null, contentLength = null;
FileOutputStream fout = null;
long THREAD_SLEEP_TIME = 20;
int BUFFER_SIZE = 65535;
long FILE_SIZE_LIMIT = 25000000;
try {
System.out.println( "The Client "+
connectedClient.getInetAddress() + ":" + connectedClient.getPort() + " is connected");
inFromClient = new DataInputStream(connectedClient.getInputStream());
outToClient = new DataOutputStream(connectedClient.getOutputStream());
connectedClient.setReceiveBufferSize(BUFFER_SIZE);
currentLine = readLine();
String headerLine = currentLine;
StringTokenizer tokenizer = new StringTokenizer(headerLine);
String httpMethod = tokenizer.nextToken();
String httpQueryString = tokenizer.nextToken();
System.out.println(currentLine);
if (httpMethod.equals("GET")) { // GET Request
System.out.println("GET request");
if (httpQueryString.equals("/")) {
// The default home page
String responseString = HTTPFileUploadServer.HTML_START +
"<form action=\"http://127.0.0.1:5000\" enctype=\"multipart/form-data\"" +
"method=\"post\">" +
"Enter the name of the File <input name=\"file\" type=\"file\"><br>" +
"<input value=\"Upload\" type=\"submit\"></form>" +
"Upload only text files." +
HTTPFileUploadServer.HTML_END;
sendResponse(200, responseString , false);
} else {
sendResponse(404, "<b>The Requested resource not found ...." +
"Usage: http://127.0.0.1:5000</b>", false);
}
} //if
else { //POST Request
System.out.println("POST request");
while(true) {
currentLine = readLine();
if (currentLine.indexOf("Content-Type: multipart/form-data") != -1) {
postBoundary = currentLine.split("boundary=")[1];
// The POST boundary
while (true) {
currentLine = readLine();
if (currentLine.indexOf("Content-Length:") != -1) {
contentLength = currentLine.split(" ")[1];
System.out.println("Content Length = " + contentLength);
break;
}
}
if (Long.valueOf(contentLength) > FILE_SIZE_LIMIT) {
inFromClient.skip(Long.valueOf(contentLength));
sendResponse(200, "File size should be less than 25MB", false);
break;
}
.........................................
sendResponse(200, "File " + filename + " Uploaded..", false);
fout.close();
break;
} //if
}//while (true); //End of do-while
}//else
//Close all streams
System.out.println("Closing All Streams....");
closeStreams();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done....");
}
public void sendResponse(int statusCode, String responseString, boolean isFile) throws Exception {
............................................................}
public static void main (String args[]) throws Exception {
ServerSocket Server = new ServerSocket (5000, 10, InetAddress.getByName("127.0.0.1 "));
System.out.println ("HTTP Server Waiting for client on port 5000");
//Create a new thread for processing clients
while(true) {
Socket connected = Server.accept();
(new HTTPFileUploadServer(connected)).start();
}
}
}
Thanks in advance