Quote:
quote:Originally posted by Mr. Ram
Raj,
Pls let me know the command, how u r executing this program. I am trying as below :
java UploadFile classpath.txt \\sys1203\\Ram
|
Hi Mr. Ram,
In fact the code that you used was just a part of my whole project, that's why there were some missing statements in the code. If you interested in such kindly go through the following java source files that are properly tested and complete in itself while other things remain the same.
UploadFile.java
import java.io.*;
import java.net.*;
public class UploadFile
{
URLConnection con;
public static void main(String []str)
{
try
{
File srcFile = new File(str[0]);
String destPath = str[1];
new UploadFile().transferFile(srcFile, destPath);
}
catch(Exception e)
{
System.out.println("Usage: sourcePath destPath");
System.exit(1);
}
}
public void transferFile(File sourceFile, String destPath)
{
try
{
String base = "http://127.0.0.1:8080/test/servlet/RecvServlet_data";
String fileName = sourceFile.getPath();
destPath += File.separator + sourceFile.getName();
FileInputStream fin = new FileInputStream(sourceFile);
URL url = new URL(base + "?name=" + fileName + "&path=" + destPath);
con = url.openConnection();
con.setRequestProperty("REQUEST_METHOD", "POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(true);
con.setRequestProperty("Content-Type", "multipart-formdata");
DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
int c = 30720;
byte b[] = new byte[c];
for(int i = fin.read(b, 0, c); i >= 0; i = fin.read(b, 0, c))
{
dataOut.write(b, 0, i);
}
dataOut.close();
fin.close();
InputStream input = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(input));
String strRes = null;
while((strRes = br.readLine()) != null)
System.out.println(strRes);
input.close();
br.close();
}
catch(Exception e)
{
System.out.println("Error: " + e.toString());
}
finally
{
con = null;
}
}
}
RecvServlet_data.java (Servlet File)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RecvServlet_data extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String fileName = req.getParameter("name");
String destIP = req.getParameter("path");
res.setContentType("text/plain");
PrintWriter pw = res.getWriter();
System.out.println("File : " + fileName + " is ready to transfer to " + destIP);
File destFile = new File(destIP);
DataInputStream dataIn = new DataInputStream(req.getInputStream());
BufferedOutputStream buffOut = new BufferedOutputStream(new FileOutputStream(destFile));
int c = 30720;
byte b[] = new byte[c];
try
{
for(int j = dataIn.read(b, 0, c); j >= 0; j = dataIn.read(b, 0, c))
{
buffOut.write(b, 0, j);
}
dataIn.close();
buffOut.close();
System.out.println("Now, done...");
pw.write("File transfer completed");
}
catch(Exception e)
{
System.out.println("Error in Servlet : " + e.getMessage());
pw.write("Error in transferring file...");
}
}
}
Now try to execute UploadFile.java by giving the absolute paths rather than relative paths.
eg. - java UploadFile c:\\classpath.txt d:\\test
If still getting any problem, let me know.
Raj