File upload, download, delete function

1. File upload
 front-end code:

1 HTML Code: <% - layui comes with the original file upload control - %> 
2  < the INPUT of the type = "File" name = "File" the above mentioned id = "Test20" > 
3 JS Code: <% - the Accept as upload the specified format, file type to all files, url is uploaded back to the path - %> 
. 4  layui.use ( 'Upload', function () {
 . 5  var $ = layui.jquery
 . 6  , Upload = layui.upload;
 7  // binding domain of the original document
 . 8  upload.render ({
 . 9  elem: '# Test20'
 10  , Accept: 'file'
 . 11  , URL: '
$ {Ctx} / fpPlaceFile / uploadFile / $ {} placeId '12 });
13 });

Background Code:

// the Controller layer uses MultipartFile file parameter accepts foreground pass over file information 
public  int uploadFile (MultipartFile MFILE, the HttpServletRequest Request, strPath String, String relativedId, strRelativedType String, String strDesc) 
 throws Exception {
 // obtain user information 
Subject Subject = SecurityUtils. the getSubject (); 
MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) subject.getPrincipal (); 
String the userName = shiroUser.getUser () getStrAccount ();. 
the User User = shiroUser.getUser ();
 // the specified file stored relative path 
String = contentUploadPath "/ static / UploadFiles /" + strPath;
 // absolute path
AppPath = String .. Request.getSession () getServletContext () getRealPath (contentUploadPath); 
String strFileName = "" ; 
String strFilePath = "" ;
 // determine whether the file is empty 
IF (! MFile.isEmpty ()) {
 // supplement absolute path 
file uploadFileDirct = new new file (appPath, userName + "/" + relativedId);
 // path is empty, then create the path 
IF (! uploadFileDirct.exists ()) { 
uploadFileDirct.mkdirs (); 
} 
// get the file name 
strFileName = mFile.getOriginalFilename ();
 // get the file format
String suffixName = strFileName.substring(strFileName.lastIndexOf(".") + 1);
String fileName = strDesc+strFileName;
File uploadFile = new File(uploadFileDirct, fileName);
//补充相对路径
strFilePath = contentUploadPath + "/" + userName + "/" + relativedId + "/" + fileName;
//上传文件
mFile.transferTo(uploadFile);
NjAttach njAttach = new NjAttach();
njAttach.setId(fid);
njAttach.setStrName(strFileName);
BigDecimal b = new BigDecimal((double) mFile.getSize() / 1024);
doubleb.setScale = fileSize (2 , BigDecimal.ROUND_HALF_UP) .doubleValue (); 
njAttach.setDoubleSize (fileSize); 
njAttach.setStrDocType (suffixName); 
njAttach.setRelativedId (relativedId); 
njAttach.setStrRelativedType (strRelativedType); 
njAttach.setStrDesc (strDesc ); 
njAttach.setStrPath (strFilePath); 
njAttach.setCreateUser (User); 
njAttach.setCreateDateTime (DateTools.getFullNowDateTime ()); 
return  the this .addAttach (njAttach); 
} the else {
 the throw  new new a ServiceException ( "failed to upload the file is empty or not select a file! " ); 
} 
}

Delete Files

Front Code:

1 <a class="delete btn-small" target="dialog" 
2 dialogId="upadteFcHydrantDialog22" href="${ctx}/fpPlaceFile/delete/${fpPlaceFile.id}">删除</a>

Background Code:

1  // get the file to the absolute path and file name stored in the server 
2 String url = fpPlaceFileService.findOneById (the above mentioned id) .getFileUrl ();
 3 String name = fpPlaceFileService.findOneById (the above mentioned id) .getUid ();
 4 String affiPath = + URL "/" + name;
 . 5 File pdfFile = new new File (.. Request.getSession () GetServletContext () the getRealPath (affiPath)); 
 . 6 pdfFile.delete ();

document dowload

Reception codes, no code behind:

1  <% - direct access to the file path and file name, url by downloading, if the file is an image directly open - %> 
2  < A class = "downloads BTN-Small"  
. 3  the href = "$ {$} CTX fpPlaceFile.fileUrl} {/} $ {fpPlaceFile.uid " > download </ A >

Guess you like

Origin www.cnblogs.com/zeevy/p/12118064.html