About image upload tools to write

 1 package obj.controller;
 2 import java.io.File;
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.UUID;
 6 import javax.servlet.http.HttpServletRequest;
 7 import org.springframework.web.multipart.MultipartFile;
 8 
 9 public class upload2 {
10    public List<String> uploadfile(HttpServletRequest request, MultipartFile[] files) {
11       //返回所有上传成功的图片路径集合
12       List<String> list = new new the ArrayList <String> ();
 13 is  
14        // pictures stored path, upload files stored at the server tomcat path 
15        String savepath = request.getServletContext () the getRealPath ( "/ serverimg." );
 16        System.out.println ( savepath);
 . 17  // F.: \ Eclipse \ Workspace \ .metadata \ .plugins \ org.eclipse.wst.server.core \ TMP1 \ wtpwebapps \ SpringMVC \ serverimg 
18 is  
. 19        File File = new new File (savepath);
 20 is        // If the directory does not exist 
21 is        IF (! File.Exists ()) {
 22 is           // Create a directory 
23 is           file.mkdirs ();
 24        }
25  
26 is        IF (Files =! Null && files.length> 0 ) {
 27           for ( int I = 0; I <files.length; I ++ ) {
 28              // get uploaded file name 
29              String imgName = Files [I]. getOriginalFilename ();
 30              IF (! imgName = null ! && imgName = "" ) {
 31 is                 // extension obtained uploaded file 
32                 String fileExtName = imgname.substring (imgname.lastIndexOf ( "." ));
 33 is                 // save the documents were generated uuid 
34                 String filename =makeFileName (fileExtName);
 35                 // images stored path 
36                 String filePath = savepath + "/" + filename;
 37 [  
38 is                 // access path to the image, can be directly put into the database displayed image path
 39                 // String the basePath = " HTTP: localhost // : 8080 "+ request.getContextPath () +" / serverimg / ";
 40                 // String path = basePath +" / "+ filename;
 41                 // access path to the image, put into the database after upload pictures name, need to write their own path to the front desk 
42                 String path = filename;
 43  
44                 File savedir = new new File (filePath);
 45                 IF (!saveDir.getParentFile().exists()) {
46                  saveDir.getParentFile().mkdirs();
47                }
48 
49                // 转存文件
50                try {
51                  files[i].transferTo(saveDir);
52                } catch (Exception e) {
53                  // TODO Auto-generated catch block
54                  e.printStackTrace();
55                }
56                list.add(path);
57             }
58          }
59       }
60       returnList;
 61 is     }
 62 is  
63 is     Private String makeFileName (String fileExtName) {
 64        // To prevent the phenomenon file cover, to upload files to generate a unique file name 
65        return UUID.randomUUID () toString () Replace (. ". - "," ") + fileExtName;
 66     }
 67  
68  }
 69  

Write the front page

$(document).ready(function(){  
$('.jtt').get(0).onchange=function (e){
       var that=this;
       console.dir(this)
       var read=new FileReader();
       read.readAsDataURL(this.files[0]);
       console.log($('.sh2').get(0).src)
       read.onload=function () {
           $('.sh2').get(0).src=this.result;
       }  
       
       var formData = new FormData($("#form")[0]);

       
       $.ajax({
          URL: 
                the console.log (obj)'<% = Request.getContextPath ()%> / Banner / IMG' , 
          type: "the POST" , 
          Data: formData, 
          dataType: "JSON" , 
          the processData: false , // must avoid false jQuery will default to the formdata processing 
          contentType: false , // must be false will automatically add the right-Type Content 
          cache: false , // to true, then the cache will read 
          Success: function (the Data) { 
              console.log (the Data); 
            $ .each (the Data, function (I, obj) { 
                $ ("#form").append('<input type="text" name="banner" value="'+(obj)+'">');
            })
          }
       });
       
   }
})

Dependent jar package

<-! Jsp dependent must be introduced, or visit the jsp page will be saved as -> 
        <dependency> 
            <groupId> org.apache.tomcat.embed </ groupId> 
            <artifactId> Tomcat-embed-Jasper </ artifactId> 
            <scope > Provided </ scope> 
        </ dependency>

 

Guess you like

Origin www.cnblogs.com/yangfanfan/p/11574210.html