简体   繁体   English

如何知道一个jsp中有多少个对象类型=“文件”

[英]how to know how many object type=“file” there are in a jsp

i'm created a form to upload images into a blob field of a mysql database. 我创建了一个表单,用于将图像上传到mysql数据库的blob字段中。

In a servlet i get the imagine inside a type="file" field in a jsp page. 在一个servlet中,我在一个jsp页面的type =“ file”字段中得到了想象。

  Part filePart = request.getPart("Name_of_the_FILE_fields");  

Now i want to allow user to upload more images at the same time, so i put in my jsp page a lot of type="file" field. 现在,我想允许用户同时上传更多图像,因此我在jsp页面中放置了许多type =“ file”字段。

I thought that i could do something like this 我以为我可以做这样的事情

 Part filePart[] =request.getParameterValues("Name_of_the_FILE_fields");

but of course this is not the right way to do it. 但这当然不是正确的方法。

Here's a script that you can use. 这是您可以使用的脚本。

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

    String savePath = request.getServletContext().getRealPath("") + File.separator + "files";       

    File fileSaveDir = new File(savePath);
    if (!fileSaveDir.exists()) {
        fileSaveDir.mkdir();
    }

    for (Part part : request.getParts()) {
        String fileName = findFileName(part);
        part.write(savePath + File.separator + fileName);
    }   
}

private String findFileName(Part part) {
    String[] items = part.getHeader("content-disposition").split(";");
    for (String item : items) {
        if (item.trim().startsWith("filename")) {
            return item.substring(item.indexOf("=") + 2, item.length() - 1);
        }
    }
    return "";
}

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

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