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
Spring mvc 文件上传并保存到目录中_Spring Mvc_File Upload - Fatal编程技术网

Spring mvc 文件上传并保存到目录中

Spring mvc 文件上传并保存到目录中,spring-mvc,file-upload,Spring Mvc,File Upload,我使用Spring Framework的MultipartFile允许用户上载pdf文件,我希望将该文件存储在一个目录中: 为此,我使用了方法tranferTo(),但它仍然不起作用 @RequestMapping(method = RequestMethod.POST, value = "/upload") public CV handleFileUpload(@RequestParam("file") MultipartFile file) throw

我使用Spring Framework的MultipartFile允许用户上载pdf文件,我希望将该文件存储在一个目录中:

为此,我使用了方法tranferTo(),但它仍然不起作用

    @RequestMapping(method = RequestMethod.POST, value = "/upload")
    public CV handleFileUpload(@RequestParam("file") MultipartFile file)
            throws IOException, SAXException, TikaException {

     File convFile = new File( file.getOriginalFilename());
         System.out.println(file.getOriginalFilename());
           file.transferTo(convFile );
}

如何添加目标路径?

存储在Catalina中,它是项目文件夹的父文件夹

   String rootPath = System.getProperty("catalina.home");
          File dir = new File(rootPath + File.separator + "yourfolderName");
          if (!dir.exists())
              dir.mkdirs();

          // Create the file on server
          java.util.Date date= new java.util.Date();
          String Path = dir.getAbsolutePath() + File.separator + (new Timestamp(date.getTime())).toString().replace(":", "").toString().replace(".", ".").toString().replace(" ","").toString().replace("-","").toString()+".pdf";
或存储在项目中的特定文件夹中,例如文件夹

@RequestMapping(value="/user/uploadFile", method=RequestMethod.POST)
public ModelAndView handleFileUpload(ModelMap model,@RequestParam(value="file", required = false) MultipartFile file,HttpServletRequest request){

    if (!file.isEmpty()) {
        //filter for checking file extewnsion
        if(file.getContentType().equalsIgnoreCase("image/jpg") || file.getContentType().equalsIgnoreCase("image/jpeg")){
            //if file is >2 MB or < 2MB
            double size = file.getSize();
            double kilobytes = (size / 1024);
            double megabytes = (kilobytes / 1024);
            if(megabytes<2){
        try {
            byte[] bytes = file.getBytes();
            String filePath = request.getRealPath("/")+"yourFolderName\\ProfileImages\\"+SessionManagement.getUserName()+".jpg";
            BufferedOutputStream stream =
                    new BufferedOutputStream(new FileOutputStream(new File(filePath)));
            stream.write(bytes);
            stream.close();

            //console call
        }
        else{
            model.put("error", "Please select File less than 2 MB");
            return new ModelAndView("uploadPhotoTile");
        }
        }else{
            model.put("error", "Please select JPEG File");
            return new ModelAndView("uploadPhotoTile");
        }
    } else {
        model.put("error", "Please select File");
        return new ModelAndView("uploadPhotoTile");
    }
}
@RequestMapping(value=“/user/uploadFile”,method=RequestMethod.POST)
public model和view handleFileUpload(ModelMap model,@RequestParam(value=“file”,required=false)多部分文件文件,HttpServletRequest){
如果(!file.isEmpty()){
//用于检查文件扩展的筛选器
if(file.getContentType().equalsIgnoreCase(“image/jpg”)| | file.getContentType().equalsIgnoreCase(“image/jpeg”)){
//如果文件大于2MB或小于2MB
double size=file.getSize();
双千字节=(大小/1024);
双兆字节=(千字节/1024);
如果(兆字节)