文件上传路径

时间:2013-04-15 06:54:48

标签: jsp servlets

我正在使用commons API上传文件。根据下面列出的代码,文件会上传到应用程序的文件夹。如何强制将其上传到我选择的位置?

package fileUpload;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;




public class Upload extends HttpServlet
  {


    private static final long serialVersionUID = 1L;

private static  final String UPLOAD_DIRECTORY = "upload";


private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 3;  // 3MB
private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; // 50MB


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

    if (!ServletFileUpload.isMultipartContent(request)) {

        PrintWriter writer = response.getWriter();
        writer.println("Error: Form must has enctype=multipart/form-data.");
        writer.flush();
        return;
    }


    DiskFileItemFactory factory = new DiskFileItemFactory();

    factory.setSizeThreshold(MEMORY_THRESHOLD);

    factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

    ServletFileUpload upload = new ServletFileUpload(factory);


    upload.setFileSizeMax(MAX_FILE_SIZE);


    upload.setSizeMax(MAX_REQUEST_SIZE);




    String uploadPath = getServletContext().getRealPath("")
            + File.separator + UPLOAD_DIRECTORY;


    File uploadDir = new File(uploadPath);
    if (!uploadDir.exists()) {
        uploadDir.mkdir();
    }

    try {


        List<FileItem> formItems = upload.parseRequest(request);

        if (formItems != null && formItems.size() > 0) {

            for (FileItem item : formItems) {

                if (!item.isFormField()) {
                    String fileName = new File(item.getName()).getName();
                    String filePath = uploadPath + File.separator + fileName;
                    File storeFile = new File(filePath);


                    item.write(storeFile);
                    request.setAttribute("message",
                        "Upload has been done successfully!");
                }
            }
        }
    } catch (Exception ex) {
        request.setAttribute("message",
                "There was an error: " + ex.getMessage());
    }

    getServletContext().getRequestDispatcher("/message.jsp").forward(
            request, response);
}
}

1 个答案:

答案 0 :(得分:0)

我在做的是:

private static  final String UPLOAD_DIRECTORY = "";

currentExecutablePath = System.getProperty("user.dir");
UPLOAD_DIRECTORY = currentExecutablePath.
                       substring(0,lastIndexOfSeparator) + "yourpath";