使用“multipart / form-data”上传文件,在Controller Error中检索隐藏的变量值

时间:2013-10-15 19:49:39

标签: java html jsp servlets

我的任务是上传一个文件,当有两个应用程序(app)有不同类型的上传时,一个app叫做APA,其他就是fSCG。当我删除如果APA其他SCG代码工作,如果我使用如果APA其他SCG我得到null错误。请参阅以下代码

在我的JSP中,代码位于 -

之下
<form name="upload" action="/call" enctype= "multipart/form-data" method="POST">
    Select file: <input type="file" name="file"/>
    <input type="hidden" name="app" value="${app}"/>
    <input type="submit" value="Upload"/>
</form>

这是java控制器类

@RequestMapping(value = "/call", method = RequestMethod.POST)
public void process(HttpServletRequest request, HttpServletResponse response) throws Exception {
    final String app = request.getParameter("app");
    **if (app.equals(APA) {**  This is where the error is 


        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
         response.setContentType("text/html");
          java.io.PrintWriter out = response.getWriter( );

        if (!isMultipart) {

             return;

        }
         out.println("<h2> File is successfully uploaded<h2>"); 
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        try {
            // Parse the request
            List items = upload.parseRequest(request);
            Iterator iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();

                if (!item.isFormField()) {
                    String fileName = new File(item.getName()).getName();
                    String uploadFolder = "C:\\Users;
                    String filePath = uploadFolder + File.separator + fileName;
                    File uploadedFile = new File(filePath);

                    item.write(uploadedFile);

                }
            }

        } catch (FileUploadException ex) {
            throw new ServletException(ex);
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

else if 

if(app.equals(SCG)){**这里错误 上传的代码几乎与上面相同 }

为什么我无法使用if app上传请告诉我一些可能的例子

2 个答案:

答案 0 :(得分:0)

为了在Spring中访问上传的文件,您必须在MVC配置文件(又名bean.xml)中添加以下字段:

<!-- Multipart resolver to be used for file uploading mechanisms -->
<bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="1000000"/>
</bean>

然后,您可以在Controller课程中访问上传文件的内容和元数据:

@RequestMapping(value = "register")
public void register(MultipartFile uploadedFile){
    System.out.println(uploadedFile.getContentType());
    System.out.println(uploadedFile.getOriginalFileName());
    System.out.println(uploadedFile.getSize());

    byte[] fileContents = uploadedFile.getBytes();
}

答案 1 :(得分:0)

试试这个

@RequestMapping(value = "register")
public void register(@RequestParam(value="file") MultipartFile   uploadedFile,@RequestParam(value="app")String app){
    System.out.println(uploadedFile.getContentType());
    System.out.println(uploadedFile.getOriginalFileName());
    System.out.println(uploadedFile.getSize());
}