文件上传
Controller
@RequestMapping ( "/upload" )
@ResponseBody
public Map < String , String > uploadImg ( @RequestParam ( "file" ) MultipartFile file, Model model, HttpServletRequest request) throws IOException {
return fileService. upfile ( file, request) ;
}
Service层
public Map < String , String > upfile ( @RequestParam ( "file" ) MultipartFile file, HttpServletRequest request) throws IOException {
Map < String , String > map = new HashMap < > ( ) ;
map. put ( "code" , "1" ) ;
map. put ( "msg" , "上传文件失败" ) ;
String fileName = System . currentTimeMillis ( ) + file. getOriginalFilename ( ) ;
InputStream inputStream = file. getInputStream ( ) ;
String filePath = null ;
Boolean flag = ftpUtil. uploadFile ( fileName, inputStream) ;
if ( flag == true ) {
filePath = ftpUtil. FTP_BASEPATH + fileName;
map. put ( "code" , "0" ) ;
map. put ( "msg" , "上传文件成功" ) ;
map. put ( "fileName" , fileName) ;
}
map. put ( "path" , filePath) ;
System . out. println ( map) ;
return map;
}
FtpUtil
@Component
public class FtpUtil {
private static final String FTP_ADDRESS = "192.168.111.136" ;
private static final int FTP_PORT = 21 ;
private static final String FTP_USERNAME = "lichenyang" ;
private static final String FTP_PASSWORD = "mxy480225354" ;
public final String FTP_BASEPATH = "/home/ftp/data" ;
public boolean uploadFile ( String originFileName, InputStream input) {
boolean success = false ;
FTPClient ftp = new FTPClient ( ) ;
ftp. setControlEncoding ( "utf-8" ) ;
ftp. enterLocalPassiveMode ( ) ;
try {
int reply;
ftp. connect ( FTP_ADDRESS , FTP_PORT ) ;
ftp. login ( FTP_USERNAME , FTP_PASSWORD ) ;
reply = ftp. getReplyCode ( ) ;
System . out. println ( reply) ;
ftp. setFileType ( FTPClient . BINARY_FILE_TYPE ) ;
ftp. setFileTransferMode ( FTP . STREAM_TRANSFER_MODE ) ;
ftp. changeWorkingDirectory ( FTP_BASEPATH ) ;
System . out. println ( originFileName) ;
boolean state = ftp. storeFile ( originFileName, input) ;
System . out. println ( ftp. getReplyString ( ) ) ;
System . out. println ( state) ;
if ( ! FTPReply . isPositiveCompletion ( reply) ) {
ftp. disconnect ( ) ;
System . out. println ( "连接失败" ) ;
return success;
}
System . out. println ( "连接成功!" ) ;
input. close ( ) ;
ftp. logout ( ) ;
success = true ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( ftp. isConnected ( ) ) {
try {
ftp. disconnect ( ) ;
} catch ( IOException ioe) {
}
}
}
return success;
}
public boolean downloadFile ( String filename, HttpServletResponse response) {
boolean flag = false ;
FTPClient ftp = new FTPClient ( ) ;
OutputStream os = null ;
InputStream inputStream = null ;
String re= null ;
try {
ftp. connect ( FTP_ADDRESS , FTP_PORT ) ;
ftp. login ( FTP_USERNAME , FTP_PASSWORD ) ;
ftp. enterLocalPassiveMode ( ) ;
ftp. changeWorkingDirectory ( FTP_BASEPATH ) ;
FTPFile [ ] ftpFiles = ftp. listFiles ( ) ;
String picType;
for ( FTPFile file : ftpFiles) {
byte [ ] bytes = file. getName ( ) . getBytes ( "ISO-8859-1" ) ;
file. setName ( new String ( bytes, "utf-8" ) ) ;
System . out. println ( "name: " + file. getName ( ) ) ;
if ( filename. equalsIgnoreCase ( file. getName ( ) ) ) {
System . out. println ( true ) ;
String fileName = file. getName ( ) ;
inputStream = ftp. retrieveFileStream ( fileName) ;
System . out. println ( "该文件的文件名:" + fileName) ;
picType = fileName. split ( "\\." ) [ 1 ] ;
BufferedImage bufferedImage = null ;
bufferedImage = ImageIO . read ( inputStream) ;
os = response. getOutputStream ( ) ;
ImageIO . write ( bufferedImage, picType, os) ;
break ;
}
}
ftp. logout ( ) ;
flag = true ;
System . out. println ( "下载文件成功" ) ;
} catch ( Exception e) {
System . out. println ( "下载文件失败" ) ;
e. printStackTrace ( ) ;
} finally {
if ( ftp. isConnected ( ) ) {
try {
ftp. disconnect ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
if ( null != os) {
try {
os. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
return flag;
}
}
图片回显到前端页面
Controller
@RequestMapping ( "/loadImg" )
@ResponseBody
public void loadImg ( HttpServletResponse response, String fileName) {
boolean flag = false ;
try {
flag = fileService. downfile ( fileName, response) ;
System . out. println ( flag) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
Service层
public boolean downfile ( String filename, HttpServletResponse response) throws IOException {
boolean flag ;
flag = ftpUtil. downloadFile ( filename, response) ;
return flag;
}
Layui前端页面
< div class = " layui-upload" >
< button type = " button" class = " layui-btn" id = " fileImage" > 上传图片</ button>
< div class = " layui-upload-list" >
< img class = " layui-upload-img" id = " demo1" width = " 100px" height = " 100px" >
< p id = " demoText" > </ p>
</ div>
< input type = " hidden" id = " imageId" >
</ div>
< script type = " text/html" id = " photo" >
< div>
< img src= "/loadImg?fileName={{d.imageId}}" width= "50px" height= "50px" / >
< / div>
</ script>
layui. use ( [ 'table' , 'layer' ] , function ( ) {
$ = layui. jquery;
table = layui. table;
layer = layui. layer
table. render ( {
elem : '#test'
, url : '/member/listJson'
, toolbar : '#toolbarDemo'
, title : '用户数据表'
, height : 500
, defaultToolbar : [ ]
, cols : [
[
{ type : 'checkbox' , fixed : 'left' }
, { field : 'dyId' , title : '党员ID' , sort : true , align : 'center' , hide : true }
, { field : 'imageId' , title : '头像' , width : 100 , align : 'center' , templet : '#photo' }
, { field : 'dyName' , title : '党员姓名' , width : 150 , align : 'center' }
, { field : 'dySex' , title : '性别' , width : 150 , align : 'center' }
, { field : 'dyRdtime' , title : '入党日期' , width : 150 , align : 'center' }
, { field : 'dyPosition' , title : '党内职务' , width : 150 , align : 'center' }
, { fixed : 'right' , title : '操作' , toolbar : '#barDemo' , width : 150 , align : 'center' }
]
]
, page : true
, id : 'MTable'
} ) ;
layui. use ( 'upload' , function ( ) {
var $ = layui. jquery
, upload = layui. upload;
var dyPhone = $ ( "#dyPhone" ) . val ( ) ;
var uploadInst = upload. render ( {
elem : '#fileImage'
, url : '/upload?dyPhone=' + dyPhone
, accept : 'file'
, size : 50000
, multiple : true
, before : function ( obj ) {
obj. preview ( function ( index, file, result ) {
$ ( '#demo1' ) . attr ( 'src' , result) ;
} ) ;
}
, done : function ( res ) {
if ( res. code > 0 ) {
return layer. msg ( '上传失败' ) ;
}
var demoText = $ ( '#demoText' ) ;
demoText. html ( '<span style="color: #4cae4c;">上传成功</span>' ) ;
var imageId = $ ( "#imageId" ) ;
imageId. attr ( 'value' , res. fileName) ;
}
, error : function ( ) {
var demoText = $ ( '#demoText' ) ;
demoText. html ( '<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>' ) ;
demoText. find ( '.demo-reload' ) . on ( 'click' , function ( ) {
uploadInst. upload ( ) ;
} ) ;
}
} ) ;
} ) ;