使用jsp Servlet上传图像,“parseRequest”错误

时间:2017-12-05 22:18:11

标签: file jsp servlets upload

我是java的新手,并了解它的概念。我遵循了如何使用jsp servlet上传图像的教程,但我在“parseRequest”上收到错误

列出项目=上传。 parseRequest (请求);

这是我的代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out=response.getWriter();

    if(!ServletFileUpload.isMultipartContent(request)) {
        out.print("Nothing to upload");
        return;
    }
    FileItemFactory itemFactory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(itemFactory);
    try {
        List<FileItem> items = upload.parseRequest(request);
        for(FileItem item:items) {
            String contentType = item.getContentType();
            if(!contentType.equals("image.png")) {
                out.print("Only PNG format are accepted.");
                continue;
            }
            File uploadDir = new File("My directory will be here !!!");
            File file  = File.createTempFile("img",".png",uploadDir);
            item.write(file);
            out.print("File Saved Successfully!!");
        }

    }catch(FileUploadException e) {
        out.print("Failed to upload the file!!!");
    }
    catch(Exception ex) {
        out.print("Can't save the file!!!");
    }
}

它应该上传图像,但我得到了这个错误 收到错误:HTTP状态500 - 内部服务器错误

0 个答案:

没有答案