Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.anthony.forumspring.controller; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; /** * * @author anthony */ @Controller public class FileUploadController { // private static final Logger logger = LoggerFactory // .getLogger(FileUploadController.class); @RequestMapping(value = "/upload", method = RequestMethod.GET) public ModelAndView uploadView() { return new ModelAndView("upload"); } @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) public @ResponseBody String uploadFildHander(@RequestParam(value = "name") 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"); 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(serverFile)); 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."; } } }