-1

I am trying to upload a CSV file to Oracle database from my client machine using the URL, but I am not able to upload. When I try to upload from my PC (Server) it uploads easily but the same link when I try in my client PC, it is unable to upload. It is giving me the following exception:

java.io.FileNotFoundException: C:\new_upload\BD_RMA_25_10_11.csv (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at upload1.UploadOutOrder.service(UploadOutOrder.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)

This is my Servlet code:

    package upload1;   
    import java.io.BufferedReader; 
    import java.io.FileReader;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.util.StringTokenizer;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class UploadOutOrder extends HttpServlet {
        public void init(ServletConfig config) throws ServletException{
        super.init(config);
        System.out.println("The UploadOutOrder initiated.");
         }
        public void service(HttpServletRequest request, HttpServletResponse    response)         throws ServletException,IOException {
        String filecsv=request.getParameter("csvfile");
        Connection con=DBConnection1.getConnection();
        System.out.println("connection=----------->"+con);
        PreparedStatement pstmthdr=null;
        int rowshdr=0;
        BufferedReader brcsv=new BufferedReader(new FileReader(filecsv));
        System.out.println("reading the file");
        String strLineCsv="";
        String csvstr="";
        StringTokenizer stcsv=null;
        int lineNumberHdr=0;
        try{
            pstmthdr=con.prepareStatement("insert into Out_Table values (?)");
            System.out.println("statement executed");
            while((strLineCsv=brcsv.readLine())!=null){
                System.out.println("HEADERLINE"+strLineCsv);
                int i=1;
                if(!(lineNumberHdr==0)){
                    stcsv=new StringTokenizer(strLineCsv,",");
                    while(stcsv.hasMoreTokens()){
                        csvstr=stcsv.nextToken();
                        System.out.println("HeaderString: "+csvstr);
                        pstmthdr.setString(i++,csvstr);
                        System.out.println("below insertion");
                    }
                    rowshdr=pstmthdr.executeUpdate();
                    System.out.println(rowshdr+" rows updated.");
                }
                lineNumberHdr++;
            }
            System.out.println("not in detail");

            System.out.println("ps executed");

        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
        finally
        {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        response.sendRedirect("http://ipaddress:8080/InvenApplication/success1.jsp");
    }
}

How is this problem caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sailaja
  • 379
  • 6
  • 15
  • 26

1 Answers1

1

You were using MSIE browser which has a bug that it sends the absolute file path instead of alone the file name in an <input type="file">. You are in the server side grabbing the absolute file path as parameter and then using this to look for a file on the server side's disk file system. This would obviously only work when both the webserver and webbrowser happen to run on physically the same machine.

When the webserver runs on a physically different machine, then that absolute file path does not necessarily point to an existing file on the machine's local disk file system. If it were possible that the server could access the client's local disk file system by having just an absolute file path, then it would have been a huge security hole! Every website which you visit would then be able to leech/manipulate the disks of the website visitors! No, fortunately this isn't possible.

You should not be interested in the uploaded file's absolute path as it is in the client side. You should rather be interested in the uploaded file's content. To get the file's content, you should ensure that the encoding type of the HTML <form> is set as follows:

<form action="upload" method="post" enctype="multipart/form-data">

This way you can use Apache Commons FileUpload in the servlet to get the file's content as an InputStream. You can use InputStreamReader to decorate it into a Reader.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • BalusC is right; in fact request.getParameter("csvfile"); will give the path on the client. Apache Commons FileUpload is the way to go, as doing it yourself requires astonishly much coding. – Joop Eggen Oct 27 '11 at 12:57