#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    0
    Rep Power
    0

    Hi:
    can anyone help me with the transfer of afile from one machine to another using servlets. I want a java program which when run from the dos prompt would transfer a file on my machine (location is hardcoded) to another machine and save it in a particular folder on that machine. both the machines are in the same network.

    naren

    Comments on this post

    • JimmyGosling agrees
    • Gran Roguismo agrees
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    0
    Rep Power
    0
    Naren,
    If u are looking to upload a file to another machine/server there are a couple of ways to go about it.
    1) Jason hunter has a mutipart request/response @ www.servlets.com( for Form based file uploads)
    2) You can use this code which is given in the o'reilly book

    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class UploadServlet extends HttpServlet {
    static final int MAX_SIZE=10240000;
    String rootPath, successMessage;

    public void init(ServletConfig config) throws ServletException
    {
    super.init(config);
    rootPath=config.getInitParameter("RootPath");
    if (rootPath==null)
    {
    rootPath="/";
    }
    successMessage=config.getInitParameter("SuccessMessage");
    if (successMessage==null)
    {
    successMessage="File Upload Complete";
    }
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)
    {
    ServletOutputStream out=null;
    DataInputStream in=null;
    FileOutputStream fileOut=null;
    try
    {
    response.setContentType("text/plain");
    out=response.getOutputStream();
    }
    catch(IOException ie)
    {
    System.out.println("Error getting output stream");
    System.out.println("Error description" +ie);
    return;
    }
    try
    {
    String contentType=request.getContentType();
    if (contentType!=null && contentType.indexOf("multipart/form-data")!=-1)
    {
    in = new DataInputStream(request.getInputStream());
    int formDataLength=request.getContentLength();
    if (formDataLength>MAX_SIZE)
    {
    out.println("Sorry the file is too large to upload");
    out.flush();
    return;
    }
    byte dataBytes[]=new byte[formDataLength];
    int bytesRead=0;
    int totalBytesRead=0;
    while (totalBytesRead<formDataLength)
    {
    bytesRead=in.read(dataBytes,totalBytesRead,formDataLength);
    totalBytesRead+=bytesRead;
    }
    String file=new String(dataBytes);
    dataBytes=null;
    int lastIndex=contentType.lastIndexOf("=");
    String boundary=contentType.substring(lastIndex+1,contentType.length());
    String directory="";
    if (file.indexOf("name="Directory"")>0)
    {
    directory=file.substring(file.indexOf(file.indexOf("name="Directory"")));
    directory=directory.substring(directory.indexOf("n")+1);
    directory=directory.substring(directory.indexOf("n")+1);
    directory=directory.substring(0,directory.indexOf("n")-1);
    if (directory.indexOf("..")>0)
    {
    out.println("Security Error Cannot upload to a directory higher in the tree");
    return;
    }
    }
    String successPage="";
    if (file.indexOf("name="SuccessPage"")>0)
    {
    successPage=file.substring(file.indexOf("name="SuccessPage""));
    successPage=successPage.substring(successPage.indexOf("n")+1);
    successPage=successPage.substring(successPage.indexOf("n")+1);
    successPage=successPage.substring(0,successPage.indexOf("n")-1);

    }

    String overWrite;

    if (file.indexOf("name="OverWrite"")>0)
    {
    overWrite=file.substring(file.indexOf("name="OverWrite""));
    overWrite=overWrite.substring(overWrite.indexOf("n")+1);
    overWrite=overWrite.substring(overWrite.indexOf("n")+1);
    overWrite=overWrite.substring(0,overWrite.indexOf("n")-1);

    }
    else
    {
    overWrite="false";
    }
    String overWritePage="";

    if (file.indexOf("name="OverWritePage"")>0)
    {
    overWritePage=file.substring(file.indexOf("name="OverWritePage""));
    overWritePage=overWritePage.substring(overWritePage.indexOf("n")+1);
    overWritePage=overWritePage.substring(overWritePage.indexOf("n")+1);
    overWritePage=overWritePage.substring(0,overWritePage.indexOf("n")-1);

    }

    String saveFile=file.substring(file.indexOf("filename="")+10);
    saveFile=saveFile.substring(0,saveFile.indexOf("n"));
    saveFile=saveFile.substring(saveFile.lastIndexOf("")+1,saveFile.indexOf("""));

    int pos;
    pos=file.indexOf("filename="");
    pos=file.indexOf("n",pos)+1;
    pos=file.indexOf("n",pos)+1;
    pos=file.indexOf("n",pos)+1;

    int boundaryLocation=file.indexOf(boundary,pos)-4;
    file=file.substring(pos,boundaryLocation);
    String fileName=new String(rootPath+directory+saveFile);
    File checkFile=new File(fileName);
    if (checkFile.exists())
    {
    if (!overWrite.toLowerCase().equals("true"))
    {
    if(overWritePage.equals(""))
    {
    out.println("Sorry the file already exists");
    }
    else
    {
    response.sendRedirect(overWritePage);
    }
    return;
    }
    }

    File fileDir=new File(rootPath+directory);
    if (!fileDir.exists())
    {
    fileDir.mkdirs();
    }

    fileOut=new FileOutputStream(fileName);
    fileOut.write(file.getBytes(),0,file.length());

    if(successPage.equals(""))
    {
    out.println("successMessage");
    out.println("File Written to" +fileName);
    }
    else
    {
    response.sendRedirect(successPage);
    }
    }
    else
    {
    out.println("Request not multipart/form-data");
    }
    }
    catch(Exception e)
    {
    try
    {
    System.out.println("Error in doPost");
    out.println("An Unexpected Error Occured");
    out.println("Error Description" +e);
    }
    catch(Exception f)
    {

    }
    }
    finally
    {
    try
    {
    fileOut.close();
    }
    catch(Exception f)
    {
    }
    try
    {
    in.close();
    }
    catch(Exception f)
    {
    }
    try
    {
    out.close();
    }
    catch(Exception f)
    {
    }
    }
    }
    }

    Works fine for me...
    Hope that helps

    Ajay


    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by narensv:

    Hi:
    can anyone help me with the transfer of afile from one machine to another using servlets. I want a java program which when run from the dos prompt would transfer a file on my machine (location is hardcoded) to another machine and save it in a particular folder on that machine. both the machines are in the same network.

    naren
    [/quote]

    Comments on this post

    • JimmyGosling agrees
    • Gran Roguismo agrees

IMN logo majestic logo threadwatch logo seochat tools logo