在Servlet 3.0中以编程方式访问MultiPartConfig [英] Programmatic access to MultiPartConfig in Servlet 3.0

查看:226
本文介绍了在Servlet 3.0中以编程方式访问MultiPartConfig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Servlet 3 @MultiPartConfig 注释在我的应用程序中实现文件上传。我需要在运行时设置multipart-config位置参数(而不是annotaion参数中的硬编码)。有编程访问servlet的multipart-config的API吗?

I use Servlet 3 @MultiPartConfig annotation to implement file upload in my application. I need set multipart-config location parameter at the runtime (not hardcode in annotaion parameter). Is there any API for programmatic access to multipart-config of servlet?

谢谢

推荐答案

@MultiPartConfig实际上只是容器的标记接口。初始化servlet时,提供的注释值将使用代理对象映射到它。当传入请求是multipart / form-data时,上载的部分将映射到请求,容器将根据注释中的值和请求中的部分执行必要的工作。您无法拦截此过程,因为它全部发生在容器的内部。但是,有一种选择。它需要第二次执行文件系统操作。由于您拥有所有部件,因此您可以重建文件并将其重新上传到您选择的位置。它可能看起来像下面的方法。请记住,虽然我在自己的servlet中快速测试了这个概念但它显然没有完成代码:

The @MultiPartConfig is really just a marker interface for the container. When the servlet is initialized the annotation values provided are mapped to it with a proxy object. When the incoming request is a multipart/form-data the parts of the upload are mapped to the request, and the container performs the necessary work based upon the values from the annotation and the parts on the request. There is no way for you to intercept this process since it all happens within the guts of the container. However, there is one alternative. It requires performing a file system operation a second time. Since you have all the parts you can reconstruct the file and "re-upload" it to the location of your choice. It might look something like the method below. Keep in mind although I tested this quickly in a servlet of my own to demonstrate the concept it is obviously not finished code:

@Override
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws ServletException, IOException {

    httpServletResponse.setContentType("text/html");
    PrintWriter printWriter = httpServletResponse.getWriter();

    InputStream inputStream;
    FileOutputStream fileOutputStream;

    for (Part part : httpServletRequest.getParts()) {

        inputStream = httpServletRequest.getPart(part.getName()).getInputStream();
        int i = inputStream.available();
        byte[] b = new byte[i];
        inputStream.read(b);
        String fileName = "";

        for (String temp : part.getHeader("content-disposition").split(";")) {
            if (temp.trim().startsWith("filename")) {
                fileName = temp.substring(temp.indexOf('=') + 1).trim().replace("\"", "");
            }
        }

        String uploadDir = "/temp";
        fileOutputStream = new FileOutputStream(uploadDir + "/" + fileName);
        fileOutputStream.write(b);
        inputStream.close();
        fileOutputStream.close();

        printWriter.write("Uploaded file " + uploadDir + "/" + fileName + ".");
    }
}

这篇关于在Servlet 3.0中以编程方式访问MultiPartConfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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