简体   繁体   中英

how to get the full file path from fileupload in java servlet from HTML

How can i get the file upload Full path from HTML in servlet to upload it.When i am printing the file Name to uploaded it just getting the name not the full path.I don't no how to get it. I am getting this error.

fileField!!!!!!!!  Browse 
fileName!!!!!!!!  fromJSON.csv

Jan 14, 2016 7:31:51 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [PriorityServlet] in context with path[/tc-eqcweb] threw exception
java.io.FileNotFoundException: fromJSON.csv (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.mongodb.gridfs.GridFS.createFile(GridFS.java:239)
at com.eqc.servlet.PriorityServlet.doPost(PriorityServlet.java:107)

My HTML form to upload the file

<form action="priorityServlet" method="post" enctype="multipart/form-data">
            <div class="col-sm-8 policytxt_align">
                <div class="file_1_mas">
                    <a class='btn btn-primary' href='javascript:;'>
                        <input id="file_bal" name="Browse" type="file" value="Browse" />
                    </a> &nbsp; <br />
                    <div><input type="submit" value="Upload File" onClick="getExclusion();" /> </div>

                    <div align="center"></div>
                                    <div></div>

                                </div>

My servlet is not getting the full path of file to upload:

public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException {
isMultipart = ServletFileUpload.isMultipartContent(req);
    java.io.PrintWriter out = res.getWriter();
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(maxMemSize);
    factory.setRepository(new File("D:\\temp111"));
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax(maxFileSize); 
    String fileName = null;
    String fieldName = null;
    String rawName = null;
    try {

        List fileItems = upload.parseRequest(req); 
        Iterator i = fileItems.iterator();
        while (i.hasNext()) {
            FileItem fi = (FileItem) i.next();
            if (!fi.isFormField()) {
                // Get the uploaded file parameters
                fieldName = fi.getFieldName();
                fileName = fi.getName();

            }
        }
        System.out.println("fileField!!!!!!!!  "+fieldName);
        System.out.println("fileName!!!!!!!!  "+fileName);
    } catch (Exception ex) {
        System.out.println(ex);
    }

For security reasons the browser will not tell you the path of the uploaded file. The contents of the file are in the request - use getPart

see http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html

You are forgetting the enctype attribute in your html code

<input id="file_bal" name="Browse" type="file" value="Browse" />

change it to this

<input id="file_bal" name="Browse" type="file" value="Browse" enctype="multipart/form-data"/>

only then you can read it using getPart

And @ScaryWombat specified in his post you can't get the file path and you don't need it for uploading a file, because you are reading the file in your servlet using getPart() method

Hope it helps anyone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM