先放上代码
public ServerResponse upload ( @RequestParam ( "file" ) MultipartFile file, HttpServletRequest request) {
String path= request. getSession ( ) . getServletContext ( ) . getRealPath ( "/upload" ) ;
String targetFileName= fileService. upload ( file, path) ;
String url= PropertiesUtil. getPropertity ( "ftp.server.http.prefix" ) + targetFileName;
Map fileMap= new HashMap ( ) ;
fileMap. put ( "uri" , targetFileName) ;
fileMap. put ( "url" , url) ;
return ServerResponse. success ( fileMap) ;
}
@Service
@Slf4j
public class FileServiceImpl implements IFileService {
public String upload ( MultipartFile file, String path) {
String fileName= file. getOriginalFilename ( ) ;
String fileExtensionName= fileName. substring ( fileName. lastIndexOf ( "." ) + 1 ) ;
String uploadFileName= UUID. randomUUID ( ) . toString ( ) + "." + fileExtensionName;
log. info ( "开始上传文件,上传文件名{},上传路径{},新文件名{}" , fileName, path, fileExtensionName)