2015-12-21 82 views
0

我是新的弹簧,我想在服务器上传文件。但它没有保存该特定格式的文件..帮助我....如何在服务器上使用弹簧上传文件

这里是我的jsp页面。

<html> 
<head> 
<title>Upload File Request Page</title> 
</head> 
<body> 

    <form method="POST" action="uploadFile" enctype="multipart/form-data"> 
     File to upload: <input type="file" name="file"><br><br> 
    <!--  Name: <input type="text" name="file"><br> <br>  -->     
     <input type="submit" value="Upload"> Press here to upload the file! 
    </form> 

</body> 
</html> 

和控制器文件。

public class FileUploadController 
    { 
    private static final Logger logger = LoggerFactory.getLogger(FileUploadController.class); 

    /** 
    * Upload single file using Spring Controller 
    */ 
    @RequestMapping(value = "/upload", method = RequestMethod.GET) 
    public String upload(Locale locale, Model model) 
    { 
     logger.info("upload"); 
     return "upload"; 
    } 

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) 
    public @ResponseBody String uploadFileHandler(@RequestParam(value = "name", required = false) String name, 
      @RequestParam("file") MultipartFile file) 
      { 

      if (!file.isEmpty()) 
      { 
       try 
       { 
       byte[] bytes = file.getBytes(); 

       // Creating the directory to store file 
       String rootPath = System.getProperty("catalina.home"); 
       File dir = new File(rootPath + File.separator + "tmpFiles/abc"); 
        if (!dir.exists()) 
        dir.mkdirs(); 

       // Create the file on server 
        File serverFile = new File(dir.getAbsolutePath() + File.separator + name); 
       BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(name)); 
       stream.write(bytes); 
       stream.close(); 

       logger.info("Server File Location=" + serverFile.getAbsolutePath()); 

       return "You successfully uploaded file=" + name + "!"; 
       } catch (Exception e) 
       { 
        return "You failed to upload " + name + " => " + e.getMessage(); 
       } 
      } 
      else 
      { 
       return "You failed to upload " + name + " because the file was empty."; 
      } 
     } 

回答

0

控制器页面

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST) 
public @ResponseBody String uploadFileHandler(@RequestParam(value = "name", required = false) String name, 
     @RequestParam("file") MultipartFile file) { 


    if (!file.isEmpty()) { 
     try { 
      System.out.println(">>>>"+ file.getOriginalFilename()); 
      byte[] bytes = file.getBytes(); 

      // Creating the directory to store file 
      String rootPath = System.getProperty("catalina.home"); 
      System.out.println(">"+rootPath); 
      File dir = new File(rootPath + File.separator + "tmpFiles/abc"); 
      if (!dir.exists()) 
       dir.mkdirs(); 

      // Create the file on server 
      File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename()); 
      BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile)); 
      stream.write(bytes); 
      stream.close(); 

      logger.info("Server File Location=" + serverFile.getAbsolutePath()); 

      return "You successfully uploaded file=" + file.getOriginalFilename(); 
     } catch (Exception e) { 
      return "You failed to upload " + file.getOriginalFilename() + " => " + e.getMessage(); 
     } 
    } else { 
     return "You failed to upload " + file.getOriginalFilename() + " because the file was empty."; 
    } 
} 
相关问题