Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文件上传在spring mvc和tomcat6中不起作用_Java_Spring Mvc_File Upload_Tomcat6_Tomcat8 - Fatal编程技术网

Java 文件上传在spring mvc和tomcat6中不起作用

Java 文件上传在spring mvc和tomcat6中不起作用,java,spring-mvc,file-upload,tomcat6,tomcat8,Java,Spring Mvc,File Upload,Tomcat6,Tomcat8,我和tomcat6一起在spring mvc中工作。我试图上传一个文件,并且能够使用tomcat8进行war部署 但是我在使用tomcat6部署时遇到了以下错误 org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'userId' is not present 我的完整代码如下: 在pom中添加罐子 <!-- Apache Commo

我和tomcat6一起在spring mvc中工作。我试图上传一个文件,并且能够使用tomcat8进行war部署

但是我在使用tomcat6部署时遇到了以下错误

    org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'userId' is not present
我的完整代码如下:

在pom中添加罐子

    <!-- Apache Commons Upload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.2</version>
    </dependency>

    <!-- Apache Commons Upload -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.0.1</version>
    </dependency>
}

JSP:

使用tomcat6还有其他方法可以做到这一点吗?希望有人会在这方面提出建议

提前谢谢

截图


调用jsp文件时,响应中未附加参数

看-



在这里,您试图访问userId的值,但您要查找的参数可能不可用


因此,在响应中添加参数可能会删除错误。

但是当我在浏览器中检查元素时,用户ID在页面中可用。这是否是ant解决方案?一切看起来都很好@Mary.Hansen
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000" />
    </bean>
public class UploadDocController extends AbstractController {
@Resource
private UserService userService;


@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    Long userId = userService.getLoggedInUser(request).getId();

    ModelAndView modelAndView = new ModelAndView(UploadDocView.uploadmain);
    modelAndView.addObject("userId", userId);
    return modelAndView;
}
    <form method="POST" enctype="multipart/form-data" action="upload.htm">
        File to upload: 
        <input type="file" name="file"><br /> 

        <input type="hidden" name="userId" value="${userId}"><br /> <br />

        <input type="submit" value="Upload">
    </form>
    @Controller
    public class FileUploadController {

    @Resource
    private CollectionsRepository collectionsRepository;

    @RequestMapping(value="/upload", method=RequestMethod.GET)
        public @ResponseBody String provideUploadInfo() {
            return "You can upload a file by posting to this same URL.";
        }

        @RequestMapping(value="/upload.htm", method=RequestMethod.POST)
        public ModelAndView handleFileUpload(@RequestParam(value ="userId") String userId, 
                @RequestParam(value ="file") MultipartFile file){
            System.out.println("userId--------"+userId);
            if (!file.isEmpty()) {
                try {
                    String filePath = "/home/ubuntu/analyzer/LOS/";
                    byte[] bytes = file.getBytes();
                    File newFile = new File(filePath+""+file.getOriginalFilename());
                    BufferedOutputStream stream =
                            new BufferedOutputStream(new FileOutputStream(newFile));
                    stream.write(bytes);
                    stream.close();

                    List<BankStatementError> errorList = new ArrayList<BankStatementError>();

                    Excelsheetreader esr = new Excelsheetreader();
                    List<String> listaddSN = esr.GetCalculation(Long.parseLong(userId), filePath+""+file.getOriginalFilename());

                    newFile.delete();

                    for (int i = 0; i < listaddSN.size(); i++) {
                        String bank = listaddSN.get(i);
                        BankStatementError error = collectionsRepository.getErrorByBank(bank);
                        errorList.add(error);
                    }

                    ModelAndView modelAndView = new ModelAndView(UploadDocView.uploadsuccess);
                    modelAndView.addObject("errorList", errorList);
                    return modelAndView;
                } catch (Exception e) {
                    ModelAndView modelAndView = new ModelAndView(UploadDocView.uploadexecption);
                    return modelAndView;
                }
            } else {
                ModelAndView modelAndView = new ModelAndView(UploadDocView.uploadempty);
                return modelAndView;
            }
        }
    }
    org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'userId' is not present
<input type="hidden" name="userId" value="${userId}"><br /> <br />