upload file in JSP - how to change a default path for the uploaded file

左心房为你撑大大i 提交于 2019-12-12 15:42:41

问题


I have two jps pages to handle an upload of the single file. Here is a code for selecting a file:

 org.apache.commons.io.FilenameUtils, java.util.*, 
 java.io.File, java.lang.Exception" %>
...
 <form name="uploadFile" method="POST" action="processUpload.jsp"     
 enctype="multipart/form-data">
     <input type="file" name="myfile"><br />
     <input type="submit" value="Submit" />
 </form>
 ....

//--------handle uploaded file---------------------

<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>


<%
    System.out.println("Content Type ="+request.getContentType());
    System.out.println("Cookies" + request.getCookies());

    DiskFileUpload fu = new DiskFileUpload();
    // If file size exceeds, a FileUploadException will be thrown
    fu.setSizeMax(1000000);

    List fileItems = fu.parseRequest(request);
    Iterator itr = fileItems.iterator();

    while(itr.hasNext()) {
      FileItem fi = (FileItem)itr.next();

      //Check if not form field so as to only handle the file inputs
      //else condition handles the submit button input
      if(!fi.isFormField()) {
        System.out.println("\nNAME: "+fi.getName());
        System.out.println("SIZE: "+fi.getSize());
        //System.out.println(fi.getOutputStream().toString());
        File fNew= new File(application.getRealPath("/"), fi.getName());

        System.out.println(fNew.getAbsolutePath());
        fi.write(fNew);
      }
      else {
        System.out.println("Field ="+fi.getFieldName());
      }
    }
 %>

This code put a file into my build\web folder. How to set a path to a different directory on the server (assuming the write permissions are set) ? Thanks,


回答1:


Use the following code (adapted for the user guide):

// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(dir);

// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload(factory);

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);



回答2:


To set a file upload path:

<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File('/path/to/store/files'));
DiskFileUpload fu = new DiskFileUpload(factory);



回答3:


Why simply not specify the path when creating the file? You could set the path as part of your application configuration (JNI), or as a system property when the web server starts (using the -Dpath=...), and reading it using System.getProperty("path"). You could even use an environment variable defined on your system and read that environment variable using the System.getenv method.

Alternativaly, you could create just a temporary file using the File.getTempFile method. If you just need to save the file to do something with it and never use it again, that's a better option - nevertheless, you have to delete the file yourself after you use it.




回答4:


... try something like this:

File newFile = new File(request.getSession().getServletContext().getRealPath("/someUploadDirectoryOnServer/"), multipartFile.getOriginalFilename());


来源:https://stackoverflow.com/questions/1233639/upload-file-in-jsp-how-to-change-a-default-path-for-the-uploaded-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!