0

好吧,我的任务是在两个应用程序(应用程序)必须上传不同类型的应用程序时上传文件,一个应用程序称为 APA,另一个是 fSCG。当我删除 if APA else SCG 时,如果我使用 if APA else SCG,则代码有效,我得到空错误。请看下面的代码

在我的 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)) {** Error here code to upload几乎与上面相同}

为什么我不能用如果应用程序上传请给我一些可能的例子

4

2 回答 2

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());
}
于 2013-10-16T14:46:36.070 回答
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();
}
于 2013-10-15T20:41:03.793 回答