0

我正在尝试上传带有一些表单字段值的文件。文件上传工作,但我无法读取表单字段值:

req.getParameter("residentId") //returns NULL

residentId 已正确发送到服务器,我可以在 Net / request Post 选项卡的 firebug 中看到它。

这是我的服务器代码:

   @Override
public void handleRequest (HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

    if (req.getMethod().equals("POST") && ServletFileUpload.isMultipartContent(req)) {

        Long residentId = Long.parseLong(req.getParameter("residentId")); //NOT WORKING

        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        try {
            List<FileItem> items = upload.parseRequest(req);
            for (FileItem item : items) {

                String fileName = item.getName();

                File uploadedFile = new File(uploadDirStr, fileName);

                if (uploadedFile.createNewFile()) {
                    item.write(uploadedFile);

                    resp.setStatus(HttpServletResponse.SC_CREATED);
                    resp.getWriter().print(
                            "Upload success");
                    resp.flushBuffer();
                    ...
4

0 回答 0