11package cn .com .ttblog .ssmbootstrap_table .controller ;
22
3- import java .io .File ;
4- import java .io .IOException ;
5- import java .util .ArrayList ;
6- import java .util .Arrays ;
7- import java .util .Iterator ;
8- import java .util .List ;
9- import java .util .Map ;
10- import java .util .concurrent .TimeUnit ;
11-
12- import javax .servlet .http .HttpServletRequest ;
13- import javax .servlet .http .HttpServletResponse ;
14-
3+ import cn .com .ttblog .ssmbootstrap_table .model .FileMsgBean ;
4+ import cn .com .ttblog .ssmbootstrap_table .util .AjaxUtils ;
155import net .coobird .thumbnailator .Thumbnails ;
16-
176import org .apache .commons .codec .binary .Base64 ;
18- import org .apache .commons .lang3 .RandomUtils ;
197import org .apache .commons .lang3 .builder .ToStringBuilder ;
208import org .slf4j .Logger ;
219import org .slf4j .LoggerFactory ;
2210import org .springframework .stereotype .Controller ;
2311import org .springframework .ui .Model ;
24- import org .springframework .web .bind .annotation .ModelAttribute ;
25- import org .springframework .web .bind .annotation .RequestMapping ;
26- import org .springframework .web .bind .annotation .RequestMethod ;
27- import org .springframework .web .bind .annotation .RequestParam ;
28- import org .springframework .web .bind .annotation .ResponseBody ;
12+ import org .springframework .web .bind .annotation .*;
2913import org .springframework .web .context .request .WebRequest ;
3014import org .springframework .web .multipart .MultipartFile ;
3115import org .springframework .web .multipart .MultipartHttpServletRequest ;
3216
33- import cn .com .ttblog .ssmbootstrap_table .model .FileMsgBean ;
34- import cn .com .ttblog .ssmbootstrap_table .util .AjaxUtils ;
17+ import javax .servlet .http .HttpServletRequest ;
18+ import javax .servlet .http .HttpServletResponse ;
19+ import java .io .File ;
20+ import java .io .IOException ;
21+ import java .util .ArrayList ;
22+ import java .util .Arrays ;
23+ import java .util .List ;
24+ import java .util .Map ;
3525
3626@ Controller
3727@ RequestMapping ("/fileupload" )
@@ -45,11 +35,12 @@ public void ajaxAttribute(WebRequest request, Model model) {
4535 }
4636
4737 @ RequestMapping (method =RequestMethod .GET )
48- public void fileUploadForm () {
38+ public String fileUploadForm () {
39+ return "/user/photos" ;
4940 }
5041
5142 @ RequestMapping (value ="/upload" ,method =RequestMethod .POST )
52- public void processUpload (@ RequestParam MultipartFile file , Model model ) throws IOException {
43+ public void processUpload (@ RequestParam ( value = "file" ) MultipartFile file , Model model ) throws IOException {
5344 log .info ("文件上传信息:{}" ,ToStringBuilder .reflectionToString (file ));
5445 log .info ("文件上传,存储路径:{}" ,System .getProperty ("webapp.root" )+file .getOriginalFilename ());
5546 file .transferTo (new File (System .getProperty ("webapp.root" )+file .getOriginalFilename ()));
@@ -60,37 +51,37 @@ public void processUpload(@RequestParam MultipartFile file, Model model) throws
6051 @ ResponseBody
6152 public FileMsgBean ajaxUpload (@ RequestParam MultipartFile file , HttpServletRequest request ) throws IOException {
6253 log .info ("ajax文件上传信息:{}" ,ToStringBuilder .reflectionToString (file ));
63- String filename =System .getProperty ("webapp.root" )+File .separator +"image" +File .separator +file .getOriginalFilename ();
64- String relativePath =request .getServletContext ().getRealPath ("image" )+File .separator ;
54+ String dir =System .getProperty ("webapp.root" )+File .separator +"image" +File .separator ;
6555 String originName =file .getOriginalFilename ();
6656 String ext =originName .split ("\\ ." )[1 ];
6757 log .info ("file.getName():{},file.getOriginalFilename():{},originName.split(\" .\" ):{}" ,file .getName (),file .getOriginalFilename (),Arrays .deepToString (originName .split ("\\ ." )));
6858 String usedName =Base64 .encodeBase64String (originName .getBytes ())+"." +ext ;
69- String savePath = relativePath +usedName ;
59+ String saveImagePath = dir +usedName ;
7060 String host =request .getHeader ("host" );
7161 String contextPath =request .getContextPath ();
72- String url =request .getScheme ()+"://" +host +contextPath +"/image/" +usedName ;
73- log .info ("host:{},contextPath:{},relativePath:{},savePath:{}" ,host ,contextPath ,relativePath ,savePath );
62+ String accessRelativeUrl =request .getScheme ()+"://" +host +contextPath +"/image/" ;
63+ String imageUrl =accessRelativeUrl +usedName ;
64+ log .info ("host:{},contextPath:{},saveImagePath:{}" ,host ,contextPath ,saveImagePath );
7465 //缩率图
75- File saveFile =new File (savePath );
76- log .info ("ajax文件上传:{},存储路径:{},url:{}--name:{}" ,ToStringBuilder . reflectionToString ( new File ( filename )), filename , url ,file .getName ());
66+ File saveFile =new File (saveImagePath );
67+ log .info ("ajax文件上传:{},存储路径:{},url:{}--name:{}" ,file , saveImagePath , imageUrl ,file .getName ());
7768 file .transferTo (saveFile );
7869 String thumbFileName =Base64 .encodeBase64String ((originName +"scale030" ).getBytes ())+"." +ext ;
79- Thumbnails .of (saveFile ).scale (0.30f ).toFile (relativePath +thumbFileName );
70+ Thumbnails .of (saveFile ).scale (0.30f ).toFile (dir +thumbFileName );
71+ String thumbnailUrl =accessRelativeUrl +thumbFileName ;
8072 FileMsgBean bean =new FileMsgBean ();
8173 bean .setName (file .getOriginalFilename ());
8274 bean .setSize (file .getSize ());
83- bean .setUrl (url );
84- bean .setThumbnailUrl (request . getScheme ()+ "://" + host + contextPath + "/image/" + thumbFileName );
85- bean .setDeleteUrl ("url" );
75+ bean .setUrl (imageUrl );
76+ bean .setThumbnailUrl (thumbnailUrl );
77+ bean .setDeleteUrl ("no delete url" );
8678 return bean ;
8779 }
8880
8981 @ RequestMapping (value ="/multiupload" , method = RequestMethod .POST )
90- public @ ResponseBody List <FileMsgBean > upload (MultipartHttpServletRequest request , HttpServletResponse response ) {
91- log .debug ("ajax多文件上传:1{},2{},getMultiFileMap:{}" ,request .getFiles ("files" ),request .getFiles ("files[]" ),request .getMultiFileMap ());
92- Map <String , MultipartFile > files =request .getFileMap ();
93- log .debug ("files:" ,files );
82+ public @ ResponseBody List <FileMsgBean > upload (MultipartHttpServletRequest request ,@ RequestParam (value = "multifile" ,required = false ) MultipartFile [] files , HttpServletResponse response ) {
83+ log .debug ("ajax多文件上传,files:{}" ,files );
84+ String dir =System .getProperty ("webapp.root" )+File .separator +"image" +File .separator ;
9485// int sleep=RandomUtils.nextInt(2, 6);
9586// log.debug("upload-sleep:{}s",sleep);
9687// try {
@@ -100,17 +91,22 @@ public FileMsgBean ajaxUpload(@RequestParam MultipartFile file, HttpServletReque
10091// }
10192 String host =request .getHeader ("host" );
10293 String contextPath =request .getContextPath ();
103- String url =request .getScheme ()+"://" +host +contextPath +"/image/backimg.jpg " ;
94+ String url =request .getScheme ()+"://" +host +contextPath +"/image/" ;
10495 List <FileMsgBean > beans =new ArrayList <FileMsgBean >(2 );
105- for (int i =0 ;i <2 ;i ++){
106- FileMsgBean bean =new FileMsgBean ();
107- bean .setName (String .valueOf (i ));
108- bean .setSize ((long )i );
109- bean .setUrl (url );
110- bean .setThumbnailUrl (url );
111- bean .setDeleteUrl (url );
112- beans .add (bean );
113- }
96+ for (MultipartFile f :files ){
97+ try {
98+ f .transferTo (new File (dir +f .getOriginalFilename ()));
99+ } catch (IOException e ) {
100+ e .printStackTrace ();
101+ }
102+ FileMsgBean bean =new FileMsgBean ();
103+ bean .setName (f .getName ());
104+ bean .setSize (f .getSize ());
105+ bean .setUrl (url +f .getOriginalFilename ());
106+ bean .setThumbnailUrl (url +f .getOriginalFilename ());
107+ bean .setDeleteUrl ("no delete url" );
108+ beans .add (bean );
109+ }
114110 return beans ;
115111 }
116112}
0 commit comments