Package XXXX; Import java. io. File; Import java. io. InputStream; Import java. util. ArrayList; Import java. util. Calendar; Import java. util. HashMap; Import java. util. List; Import java. util. Map; Import net. sf. json. JSONObject; Import org. apache. log4j. Logger; /** Skip some code here **/ @ Controller @ RequestMapping (value = "/import", method = {RequestMethod. GET, RequestMethod. POST }) Public class ImportController extends BaseController { /** Used to print logs */ Private static final Logger logger = Logger . GetLogger (SpecialController. class ); /** * Upload images * @ Param * @ Param imageFile * @ Return */ @ RequestMapping (value = "/uploadPicture", produces = "text/json ") @ ResponseBody Public String uploadPicture (@ RequestParam ("file") MultipartFile imageFile ){ Map Result = new HashMap (); If (! ImageFile. isEmpty ()){ Try { Calendar calendar = Calendar. getInstance (); // gets the current time // Time path, decompress the file, and create a folder by year, month, and day String dataPath = "/" + calendar. get (Calendar. YEAR) + "/" + (Calendar. get (Calendar. MONTH) + 1) + "/" + calendar. get (Calendar. DATE) + "/"; // Original file name String srcName = imageFile. getOriginalFilename (); // Obtain the suffix of the uploaded file String suffix = srcName. substring (srcName. lastIndexOf (".") + 1, SrcName. length (). toLowerCase (); // Generate a 32-bit random id for extracting the file directory String uuid = UUIDGenerator. generate (); // New file name, random uuid; String picName = uuid + "." + suffix; // Upload the image generation path String imgPath = ExtendedServerConfig. getInstance () . GetStringProperty ("THUMBNAIL_PATH") + ExtendedServerConfig. getInstance () . GetStringProperty ("BACKGROUND_IMG_PATH") + dataPath; // Generate the image preview path String picturePath = ExtendedServerConfig. getInstance () . GetStringProperty ("BACKGROUND_IMG_PATH") + dataPath + picName; // If the folder does not exist, create File destfile = new File (imgPath ); If (! Destfile. exists ()){ Destfile. mkdirs (); } // Write an object InputStream fi = imageFile. getInputStream (); // Upload a file and write it to the configuration folder. FileUtils. writeFile (fi, imgPath + picName ); File file = new File (imgPath + picName ); If (file. exists ()){ Result. put ("imgPath", picturePath); // The preview path of the returned image. Result. put ("ilename", srcName ); } } Catch (Exception e ){ E. printStackTrace (); } } Return JSONObject. fromObject (result). toString (); } } |