easyui+springboot+ftp实现图片(img,gif,gnp),html的上传与下载
1:pom.xml.
ftp客户端:
2:前端代码
//初始化上传
function initupload() {
$("#onlineFileName").filebox({
buttonText: ‘选择文件’,
prompt: ‘请选择上传图片(png,gif类型)’,
buttonAlign: ‘right’
});
$("#onlineFileName").textbox(“textbox”).attr(“title”, “仅支持jpg、png文件格式”);
}
// 上传
$("#btn-upload").click(function(){
Loading.showLoading("上传中,请稍后");
var file = $("#onlineFileName").filebox("getValue");
var fileType = file.slice(file.lastIndexOf(".") + 1, file.length);
if (fileType.length == 0) {
new Dialog({
mode: 'tips', //表示是提示,而不是confirm
content: '请选择上传文件' //操作失败提示内容
});
Loading.destroyLoading();
return;
}
if (["png","PNG","gif","GIF"].join("").indexOf(fileType) == -1) {
new Dialog({
mode: 'tips', //表示是提示,而不是confirm
content: '文件类型有误, 请重新上传文件' //操作失败提示内容
});
Loading.destroyLoading();
return;
}
var province;
Util.ajax.getJson(Util.constants.CONTEXT + "", {}, function (data) {
if (data.RSP.RSP_CODE == "1") {
province = data.RSP.DATA[0].provnce;
} else {
$.messager.alert('温馨提示',data.RSP.RSP_DESC);
}
}, true);
$("#updateMultiMedia").ajaxSubmit({
url: Util.constants.MULTIMEDIA_IP + "",
method: "POST",
async: true,
headers: {
"Authorization": Util.getValue()
},
data: {
"province": province
},
success: function (serviceResponse) {
serviceResponse = decodeURI(serviceResponse);
serviceResponse = JSON.parse(serviceResponse);
if (serviceResponse.RSP.rspcode == "1") {
ftpId = serviceResponse.RSP.rspdesc;
setTimeout(function(){initPhoto(ftpId)},3000);//1000毫秒=1秒后执行test方法
new Dialog({
mode: 'tips', //表示是提示,而不是confirm
tipsType: 'success', //提示类型,error表示操作失败,success表示操作成功,无此字段表示为一般提示
content: '上传成功' //操作失败提示内容
});
} else {
new Dialog({
mode: 'tips', //表示是提示,而不是confirm
tipsType: 'error', //提示类型,error表示操作失败,success表示操作成功,无此字段表示为一般提示
content: '上传失败,请重新上传!' //操作失败提示内容
});
}
$("#onlineFileName").filebox("clear");
Loading.destroyLoading();
},
error: function (data) {
new Dialog({
mode: 'tips', //表示是提示,而不是confirm
tipsType: 'error', //提示类型,error表示操作失败,success表示操作成功,无此字段表示为一般提示
content: '操作异常!' //操作失败提示内容
});
$("#onlineFileName").filebox("clear");
Loading.destroyLoading();
}
});
3:后端 MultipartFile 对象接受 - control层
@RequestMapping(value = "/upload",method = RequestMethod.POST, produces = "text/plain")
public void upload(@RequestParam(“file”) MultipartFile file,@RequestParam(“province”) String province, HttpServletResponse servletResponse) {
KcPersonalCfgResponse response = new KcPersonalCfgResponse();
try {
logger.info(“province:”+province);
response = personalCfgSV.uploadPhotoFile(file,province);
servletResponse.getWriter().write(JSONObject.toJSONString(new KcPersonalCfgServiceResponse().getSuccessResponse(response)));
} catch (Exception e) {
logger.error(“上传文件出现异常”, e);
response.setRspdesc(“上传文件出现异常!”);
response.setRspcode(WebUtil.EXCEPTION);
}
}
3.1:file 必须和 前端一致
3.2:后端代码兼任ie8 ie8不可以接受respone。
4:service层
ftpUtil.java类调用上传到ftp服务器:
FtpUtil.java类
package com.unicom.kc.manage.customization.common.ftp;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.MalformedURLException;
@Component
public class FtpUtil {
private static Logger logger = LoggerFactory.getLogger(FtpUtil.class);
private String LOCAL_CHARSET = “GBK”;
//ftp服务器地址
@Value("{ftp.port}")
private String port;
//ftp登录账号
@Value("{ftp.password}")
private String password;
//ftp登录密码
@Value("${ftp.basePath}")
private String basePath;
/**
* 初始化ftp服务器
*/
public FTPClient getFtpClient() {
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("utf-8");
try {
ftpClient.setDataTimeout(1000 * 120);
logger.info("connecting...ftp服务器:" + hostname + ":" + port);
ftpClient.connect(hostname, Integer.parseInt(port)); // 连接ftp服务器
ftpClient.login(username, password); // 登录ftp服务器
int replyCode = ftpClient.getReplyCode(); // 是否成功登录服务器
if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(
"OPTS UTF8", "ON"))) { // 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
LOCAL_CHARSET = "UTF-8";
}
if (!FTPReply.isPositiveCompletion(replyCode)) {
logger.error("connect failed...ftp服务器:" + hostname + ":" + port);
}
logger.info("connect successfu...ftp服务器:" + hostname + ":" + port);
} catch (MalformedURLException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return ftpClient;
}
/**
* 上传文件
*
* @param pathname ftp服务保存地址
* @param fileName 上传到ftp的文件名
* @param inputStream 输入文件流
* @return
*/
public boolean uploadFileToFtp(String targetDir, String fileName, InputStream inputStream) {
boolean isSuccess = false;
String servicePath = String.format("%s%s%s", basePath, "/", targetDir);
FTPClient ftpClient = getFtpClient();
try {
if (ftpClient.isConnected()) {
logger.info("开始上传文件到FTP,文件名称:" + fileName);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.makeDirectory(servicePath);
ftpClient.changeWorkingDirectory(servicePath);
//设置为被动模式(如上传文件夹成功,不能上传文件,注释这行,否则报错refused:connect )
ftpClient.enterLocalPassiveMode();
ftpClient.storeFile(fileName, inputStream);
inputStream.close();
ftpClient.logout();
isSuccess = true;
logger.info(fileName + "文件上传到FTP成功");
} else {
logger.error("FTP连接建立失败");
}
} catch (Exception e) {
logger.error(fileName + "文件上传到FTP出现异常");
logger.error(e.getMessage(), e);
} finally {
closeFtpClient(ftpClient);
closeStream(inputStream);
}
return isSuccess;
}
public void closeStream(Closeable closeable) {
if (null != closeable) {
try {
closeable.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
//改变目录路径
public boolean changeWorkingDirectory(FTPClient ftpClient, String directory) {
boolean flag = true;
try {
flag = ftpClient.changeWorkingDirectory(directory);
if (flag) {
logger.info("进入文件夹" + directory + " 成功!");
} else {
logger.info("进入文件夹" + directory + " 失败!开始创建文件夹");
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return flag;
}
//创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
public boolean CreateDirecroty(FTPClient ftpClient, String remote) throws IOException {
boolean success = true;
String directory = remote;
if (!remote.endsWith(File.separator)) {
directory = directory + File.separator;
}
// 如果远程目录不存在,则递归创建远程服务器目录
if (!directory.equalsIgnoreCase(File.separator) && !changeWorkingDirectory(ftpClient, new String(directory))) {
int start = 0;
int end = 0;
if (directory.startsWith(File.separator)) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf(File.separator, start);
String path = "";
String paths = "";
while (true) {
String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
path = path + File.separator + subDirectory;
if (!existFile(ftpClient, path)) {
if (makeDirectory(ftpClient, subDirectory)) {
changeWorkingDirectory(ftpClient, subDirectory);
} else {
logger.error("创建目录[" + subDirectory + "]失败");
changeWorkingDirectory(ftpClient, subDirectory);
}
} else {
changeWorkingDirectory(ftpClient, subDirectory);
}
paths = paths + File.separator + subDirectory;
start = end + 1;
end = directory.indexOf(File.separator, start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
return success;
}
//判断ftp服务器文件是否存在
public boolean existFile(FTPClient ftpClient, String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
if (ftpFileArr.length > 0) {
flag = true;
}
return flag;
}
//创建目录
public boolean makeDirectory(FTPClient ftpClient, String dir) {
boolean flag = true;
try {
flag = ftpClient.makeDirectory(dir);
if (flag) {
logger.info("创建文件夹" + dir + " 成功!");
} else {
logger.info("创建文件夹" + dir + " 失败!");
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return flag;
}
/**
* 下载文件 *
*
* @param pathName FTP服务器文件目录 *
* @param pathName 下载文件的条件*
* @return
*/
public boolean downloadFile(FTPClient ftpClient, String pathName, String targetFileName, String localPath) {
boolean flag = false;
OutputStream os = null;
try {
System.out.println("开始下载文件");
//切换FTP目录
ftpClient.changeWorkingDirectory(pathName);
ftpClient.enterLocalPassiveMode();
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile file : ftpFiles) {
String ftpFileName = file.getName();
if (targetFileName.equalsIgnoreCase(ftpFileName.substring(0, ftpFileName.indexOf(".")))) {
File localFile = new File(localPath);
os = new FileOutputStream(localFile);
ftpClient.retrieveFile(file.getName(), os);
os.close();
}
}
ftpClient.logout();
flag = true;
logger.info("下载文件成功");
} catch (Exception e) {
logger.error("下载文件失败");
logger.error(e.getMessage(), e);
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
return flag;
}
/*下载文件*/
public InputStream download(String ftpFile, FTPClient ftpClient) throws IOException {
String servicePath = String.format("%s%s%s", basePath, "/", ftpFile);
logger.info("【从文件服务器获取文件流】ftpFile : " + ftpFile);
if (StringUtils.isBlank(servicePath)) {
throw new RuntimeException("【参数ftpFile为空】");
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpFile = new String(servicePath.getBytes("utf-8"), "iso-8859-1");
return ftpClient.retrieveFileStream(ftpFile);
}
/**
* 删除文件 *
*
* @param pathname FTP服务器保存目录 *
* @param filename 要删除的文件名称 *
* @return
*/
public boolean deleteFile(String pathname, String filename) {
boolean flag = false;
FTPClient ftpClient = getFtpClient();
try {
logger.info("开始删除文件");
if (ftpClient.isConnected()) {
//切换FTP目录
ftpClient.changeWorkingDirectory(pathname);
ftpClient.enterLocalPassiveMode();
ftpClient.dele(filename);
ftpClient.logout();
flag = true;
logger.info("删除文件成功");
} else {
logger.info("删除文件失败");
}
} catch (Exception e) {
logger.error("删除文件失败");
logger.error(e.getMessage(), e);
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
return flag;
}
public void closeFtpClient(FTPClient ftpClient) {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
public InputStream downloadFile(FTPClient ftpClient, String pathname, String filename) {
InputStream inputStream = null;
try {
System.out.println("开始下载文件");
//切换FTP目录
ftpClient.changeWorkingDirectory(pathname);
ftpClient.enterLocalPassiveMode();
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile file : ftpFiles) {
if (filename.equalsIgnoreCase(file.getName())) {
inputStream = ftpClient.retrieveFileStream(file.getName());
break;
}
}
ftpClient.logout();
logger.info("下载文件成功");
} catch (Exception e) {
logger.error("下载文件失败");
logger.error(e.getMessage(), e);
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
return inputStream;
}
// public static void main(String[] args) {
// FtpUtil ftp =new FtpUtil();
// //ftp.uploadFile("/home/ngkm/ftptest", “test_2018_05_23.docx”, “C://test.txt”);
// //ftp.downloadFile("/home/ngkm/ftptest", “test_2018_05_23.docx”, “F://”);
// //ftp.deleteFile("/home/ngkm/ftptest", “test_2018_05_23.docx”);
// System.out.println(“ok”);
// }
}
二:下载(图片的回显)
5:前端代码
//图片回显
function initPhoto(photoId){
$(’#img_img’).attr(‘src’,Util.constants.CONTEXT + “/kc/manage/cust/msa/showphoto?photoId=”+photoId);
}
将流写在img标签的src里面。
6:后端-control层
@RequestMapping(value = “/showphoto”, method = RequestMethod.GET)
public void showPhoto(@RequestParam(“photoId”) String fileId, HttpServletResponse servletresponse) {
Response response = new Response();
servletresponse.setCharacterEncoding(“UTF-8”);
servletresponse.setContentType(“image/png;charset=UTF-8”);
servletresponse.setHeader(“contentType”, “image/png;charset=UTF-8”);
String ftpFile = String.format("%s%s%s%s", “”, “”, fileId, “.jpg”);
//文件路径
FTPClient ftpClient = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
ftpClient = ftpUtil.getFtpClient();
inputStream = ftpUtil.download(ftpFile, ftpClient);
outputStream = servletresponse.getOutputStream();
FileCopyUtils.copy(inputStream, outputStream);
} catch (Exception e) {
} finally {
ftpUtil.closeStream(outputStream);
ftpUtil.closeStream(inputStream);
ftpUtil.closeFtpClient(ftpClient);
}
}
7:service层通过 FtpUtil.java类 download方法下载