如何在web.xml中正确设置多部分配置?

时间:2018-12-08 07:55:30

标签: java mysql jsp

java.lang.IllegalStateException: Request.getPart is called without multipart 
configuration. Either add a @MultipartConfig to the servlet, or a multipart- 
config element to web.xml

当我尝试将图像插入数据库时​​出现此错误

这是我要在Java中插入的代码:

@WebServlet("/FileUploadServlet")
@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2MB
        maxFileSize = 1024 * 1024 * 10, // 10MB
        maxRequestSize = 1024 * 1024 * 50)
public class imageUpload extends HttpServlet implements insertToDB{

    private static final String SAVE_DIR = "uploads";
    public String dbFileName = "";
    java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());

    @Override
    public void insert(HttpServletRequest request, HttpServletResponse response, String title, String content, String topic, int user_id, String file) 
            throws IOException, ServletException {
        response.setContentType("text/html:charset=UTF-8");
        PrintWriter out  = response.getWriter();

        String title1 = title;
        String content1 = content;
        String topic1 = topic;
        int user = user_id;
        Part part = request.getPart(file);
        String fileName = extractFileName(part);

        String applicationPath = getServletContext().getRealPath("");

        String uploadPath = applicationPath + File.separator + SAVE_DIR;

        File fileUploadDirectory = new File(uploadPath);
        if (!fileUploadDirectory.exists()) {
            fileUploadDirectory.mkdirs();
        }
        String savePath = uploadPath + File.separator + fileName;

        String sRootPath = new File(savePath).getAbsolutePath();

        part.write(savePath + File.separator);
        File fileSaveDir1 = new File(savePath);

        dbFileName = SAVE_DIR + File.separator + fileName;
        part.write(savePath + File.separator);

        try{
            Connection conn = DatabaseConnection.getCon();
            String pst = "INSERT INTO articles(title, content, date, user_id, image, topic) VALUES (?,?,?,?,?,?)";
            PreparedStatement prep = conn.prepareStatement(pst);
            String filePath = savePath + File.separator + fileName;

            prep.setString(1, title1);
            prep.setString(2, content1);
            prep.setDate(3, sqlDate);
            prep.setInt(4, user);
            prep.setString(5, dbFileName);
            prep.setString(6, topic1);

        }catch (SQLException ex) {
            Logger.getLogger(imageUpload.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

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

}

即使我在web.xml文件中添加了多部分配置,我仍然会遇到相同的错误,即HTTP状态500错误

仅供参考 数据库中的image列是varchar,因为我想存储路径

我一直在寻找一种解决方案,但似乎找不到解决方案,我也想知道是否应该将多部分配置放在我的jsp文件或Java文件中?

这也是我的多部分配置在web.xml中的外观,由于某些原因,它不起作用:

        <multipart-config>
            <max-file-size>10485760</max-file-size>
            <max-request-size>52428800</max-request-size>
            <file-size-threshold>2097152</file-size-threshold>
        </multipart-config>

0 个答案:

没有答案