如何在JSP/Java中检索上载文件的完整路径 [英] How to retrieve full path of uploaded file in JSP/Java

查看:58
本文介绍了如何在JSP/Java中检索上载文件的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从以下HTML代码中检索要浏览的文件的完整路径:

How do i retrieve full path of the file that i'll browse from the following HTML code:

<input type="file" name="upl">

请不要提供任何脚本方法,因为现代浏览器不允许这样做!

Please, do not provide any Scripting method as Modern browsers do not allow it to do so!

推荐答案

此处是csv或xsl上传的示例

Here an exemple for a csv or xsl upload

// location to store file uploaded
private static final String UPLOAD_DIRECTORY = "ressources/csv";

//name of the uploaded file
private static final String FILE_NAME="myFileName";

// upload settings
private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 30;  // 30MiB
private static final int MAX_FILE_SIZE      = 1024 * 1024 * 400; // 400MiB
private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 500; // 500MiB 

这里有一个load()方法来调用doPost()或doGet()

And here a load() method to call in doPost() or doGet()

    protected void load(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    RequestDispatcher dispatcher;   
    dispatcher= getServletContext().getRequestDispatcher("/Connection");

    // checks if the request actually contains upload file
            if (ServletFileUpload.isMultipartContent(request)) {

                // configures upload settings
                DiskFileItemFactory factory = new DiskFileItemFactory();
                // sets memory threshold - beyond which files are stored in disk
                factory.setSizeThreshold(MEMORY_THRESHOLD);
                // sets temporary location to store files
                factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

                ServletFileUpload upload = new ServletFileUpload(factory);

                // sets maximum size of upload file
                upload.setFileSizeMax(MAX_FILE_SIZE);

                // sets maximum size of request (include file + form data)
                upload.setSizeMax(MAX_REQUEST_SIZE);

                // constructs the directory path to store upload file
                String uploadPath = getServletContext().getRealPath("")
                        + File.separator+UPLOAD_DIRECTORY;

                //Create dir if needed
                File uploadDir = new File(uploadPath);
                if (!uploadDir.exists()) {
                    uploadDir.mkdirs();
                }

                try {
                    // parses the request's content to extract file data
                    @SuppressWarnings("unchecked")
                    List<FileItem> formItems = upload.parseRequest(request);

                    if (formItems != null && formItems.size() > 0) {
                        // iterates over form's fields
                        for (FileItem item : formItems) {
                            // processes only fields that are not form fields
                            if (!item.isFormField() && item!=null) {
                                String fileName = new File(item.getName()).getName();
                                String fileNameToSave=FILE_NAME;

                                String filePath = uploadPath + File.separator + fileName;
                                String filePathToSave = uploadPath + File.separator + fileNameToSave;
                                FileNameExtensionFilter filterCSV=new FileNameExtensionFilter(null,"csv");
                                FileNameExtensionFilter filterXLS=new FileNameExtensionFilter(null,"xls");
                                File file= new File(filePath);

                                //Check file extension
                                if(filterCSV.accept(file)){

                                    File storeFile = new File(filePathToSave+".csv");                       
                                    //Save file on disk
                                    item.write(storeFile);
                                    Outils.importCSV(request);
                                }
                                if(filterXLS.accept(file)){

                                    File storeFile = new File(filePathToSave+".xls");                       
                                    //Save file on disk
                                    item.write(storeFile);
                                    Outils.importXLS(request);
                                }

                            }
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

在JSP中:

<form id="upload" enctype="multipart/form-data" 
          method="POST" action="YOUR ACTION">
    <input type="file" name="uploadFile"> 
    <input type="submit" value="Upload">
</form>

这篇关于如何在JSP/Java中检索上载文件的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆