简体   繁体   English

将Web应用程序中的文件上传到项目根文件夹

[英]Upload file in web application to project root folder

I am currently working on a web application. 我目前正在开发Web应用程序。 In some part of this application I want to upload files to a certain directory. 在此应用程序的某些部分中,我想将文件上传到某个目录。 Initially, when I wrote my test cases this worked perfectly: 最初,当我编写测试用例时,它可以很好地工作:

final static String IMAGE_RESOURCE_PATH = "res/images";

...

File directory = new File(IMAGE_RESOURCE_PATH + "/" + productId);

if(!directory.exists()) {
    directory.mkdirs();
}

This creates the directory wherein the file will be uploaded. 这将创建目录,在该目录中将上传文件。 The resulting path will be: 结果路径为:

[project root folder]/res/images/[productId] [项目根文件夹] / res / images / [productId]

Since deploying the application to a server (Tomcat 7), the directory gets created in the root of the IDE I'm using, which is a bit confusing to me. 自将应用程序部署到服务器(Tomcat 7)以来,该目录已在我正在使用的IDE的根目录中创建,这让我有些困惑。

eg: C:\\Eclipse86\\res\\images 例如:C:\\ Eclipse86 \\ res \\ images

Any idea how I can revert back to the project path with just plain Java without using some hacked technique or hard-coding the path? 知道如何使用纯Java而不使用某些技巧或对路径进行硬编码就能恢复到项目路径吗?

If you don't specify the absolute path, your directory will be created inside (or, if correctly, relative to) working directory of the application. 如果未指定绝对路径,则将在应用程序的工作目录内(或相对于工作目录而言)创建目录。

If you want to get directory inside your web application you should use getServletContext().getRealPath(String path) . 如果要在Web应用程序中获取目录,则应使用getServletContext().getRealPath(String path) For example, getServletContext().getRealPath("/") is the path to the root directory of the application. 例如, getServletContext().getRealPath("/")是应用程序根目录的路径。

To create directory with path [project root folder]/res/images/[productId] , do something like this: 要使用路径[project root folder]/res/images/[productId]创建目录,请执行以下操作:

// XXX Notice the slash before "res"
final static String IMAGE_RESOURCE_PATH = "/res/images";

...

String directoryPath = 
        getServletContext().getRealPath(IMAGE_RESOURCE_PATH + "/" + productId)
File directory = new File(directoryPath);

if(!directory.exists()) {
    directory.mkdirs();
}

Some years ago I wrote a servlet that DOWNloads a file. 几年前,我写了一个servlet来下载文件。 You could quickly refactor it for uploading. 您可以快速重构它以进行上传。 Here you go: 干得好:

public class ServletDownload extends HttpServlet {

    private static final int BYTES_DOWNLOAD = 1024;  
    public void doGet(HttpServletRequest request, 

    HttpServletResponse response) throws IOException {
        response.setContentType("text/plain");
        response.setHeader("Content-Disposition", "attachment;filename=downloadname.txt");
        ServletContext ctx = getServletContext();
        InputStream is = ctx.getResourceAsStream("/downloadme.txt");

        int read = 0;
        byte[] bytes = new byte[BYTES_DOWNLOAD];
        OutputStream os = response.getOutputStream();

        while((read = is.read(bytes))!= -1) {
            os.write(bytes, 0, read);
        }
        os.flush();
        os.close(); 
    }
}

Also, there is an easy way to get the projects' path, as new File().getAbsolutePath(). 另外,还有一种获取项目路径的简便方法,如new File()。getAbsolutePath()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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