使用servlet创建文件夹和上传文件 [英] Create folder and upload file using servlet

查看:366
本文介绍了使用servlet创建文件夹和上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个使用 tomcat 的网络项目..这是我的目录结构..

I have two web project that use tomcat..this is my directory structure..

webapps
--project1
  --WEB-INF
--project2
  --WEB-INF

我使用commons-fileupload ..这是我在project1 servlet 中的代码

I use commons-fileupload..this is part my code in servlet in project1

String fileName = item.getName();    
String root = getServletContext().getRealPath("/");
File path = new File(root + "/uploads");

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

File uploadedFile = new File(path + File.separator + fileName);
item.write(uploadedFile);

这将在'project1'中创建'uploads'文件夹,但我想创建'uploads'文件夹'webapps',因为当我取消部署'project1'时,我不希望'uploads'文件夹消失..

This will create 'uploads' folder in 'project1' but I want to create 'uploads' folder in 'webapps' because I dont want 'uploads' folder gone when I undeploy 'project1'..

我已经尝试 String root = System.getProperty (catalina.base); 但不能正常工作..

I already try String root = System.getProperty("catalina.base"); but not work..

任何人都可以帮助我...提前谢谢

Can anyone help me...thanks in advance

推荐答案

首先,在tomcat安装文件夹外的服务器中创建一个文件夹,例如 / opt / myuser / files / upload 。然后,在属性文件或web.xml中将此路径配置为Servlet init配置,以使其可用于您拥有的任何Web应用程序。

First, create a folder in your server outside the tomcat installation folder, for example /opt/myuser/files/upload. Then, configure this path in a properties file or in web.xml as a Servlet init configuration to make it available for any web application you have.

如果使用属性文件:

file.upload.path = /opt/myuser/files/upload

如果web.xml:

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>your.package.MyServlet</servlet-class>
    <init-param>
        <param-name>FILE_UPLOAD_PATH</param-name>
        <param-value>/opt/myuser/files/upload</param-value>
    </init-param>
</servlet>

或者如果您使用的是Servlet 3.0规范,则可以使用<$ c $配置init参数c> @WebInitParam 注释:

Or if you're using Servlet 3.0 specification, you can configure the init params using @WebInitParam annotation:

@WebServlet(name="MyServlet", urlPatterns = {"/MyServlet"},
    initParams = {
        @WebInitParam(name = "FILE_UPLOAD_PATH", value = "/opt/myuser/files/upload")
    })
public class MyServlet extends HttpServlet {
    private String fileUploadPath;
    public void init(ServletConfig config) {
        fileUploadPath = config.getInitParameter("FILE_UPLOAD_PATH");
    }
    //use fileUploadPath accordingly

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException) {
        String fileName = ...; //retrieve it as you're doing it now
        //using File(String parent, String name) constructor
        //leave the JDK resolve the paths for you
        File uploadedFile = new File(fileUploadPath, fileName);
        //complete your work here...
    }
}

这篇关于使用servlet创建文件夹和上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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