这里主要是讲的后台,所以前台就简单描述一下不给代码了~~~
上传:前台可以用form表单提交的方式提交图片。
展示下载:方法1:可以通过img组件的src访问后台,后台返回一个OutputStream,浏览器会自动显示为图片。至于下载嘛~~你懂得~~~;方法2:通过将图片转换为Base64码 直接返回个前台(看得见)。
删除:后台需要得到图片的路径和文件名就OK了,方法不限
后台代码:
上传:
public void upload(File file, String fileName,String dir){ //文件 上传后的文件名 上传后的路径
FTPClient ftpClient = new FTPClient();
//与ftp服务器建立连接
try {
ftpClient.setControlEncoding(Constant.FTP_ENCODING); // 中文支持
ftpClient.connect(ftpHost); //ftp服务器地址
ftpClient.login(ftpUserName, ftpPassword); //账号密码
ftpClient.setDefaultPort(ftpPort); //端口
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); //使用二进制保存方式
ftpClient.enterLocalPassiveMode(); // 用被动模式传输
ftpClient.setDefaultTimeout(defaultTimeout);// 设置默认超时时间
ftpClient.setBufferSize(1024 * 1024 * 10); // 设置缓存区
//支持多级目录 没有则创建
if(!"".equals(dir))
{
String[] listD=dir.split("/");
for(String a:listD){
boolean chagenDirFlag=ftpClient.changeWorkingDirectory(a); //寻找目录
if(chagenDirFlag==false){
boolean makeFlag= ftpClient.makeDirectory(a);
LogUtil.getAppLoger().info("创建文件目录标志:"+makeFlag);
chagenDirFlag=ftpClient.changeWorkingDirectory(a);
}
}
}
input = new FileInputStream(file);
up = ftpClient.storeFile(fileName, input);
System.out.println("上传文件返回标志:" + up + "上传目录" + dir + "文件名称" + fileName);
ftpClient.disconnect();
}catch(Exception e){
e.printStackTrace();
ftpClient.disconnect();
}
}
删除:
public boolean del(String fileName,String dir) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ip, port);
ftpClient.login(loginName, pwd);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
ftpClient.changeWorkingDirectory(dir);
// 删除
ftpClient.sendCommand("dele " + fileName + "\r\n");
} catch (SocketException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}
下载展示 方法一:OutputStream的方式
public boolean download(String fileName,String dir) {
OutputStream outputStream = ServletActionContext.getResponse.getOutputStream();
boolean flag=false;
try {
int reply;
FTPClient ftp = new FTPClient();
ftp.setControlEncoding("UTF-8"); // 中文支持
ftp.connect(ip);
ftp.login(loginName, pwd);
ftp.setDefaultPort(port);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
ftp.setDefaultTimeout(30000);// 设置默认超时时间
ftp.setBufferSize(1024*1024);//设置缓存区,默认缓冲区大小是1024,也就是1K
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
flag=ftp.changeWorkingDirectory(dir);//转移到FTP服务器目录
LogUtil.getAppLoger().info("切换目录"+flag);
try {
flag=ftp.retrieveFile(fileName, outputStream);
LogUtil.getAppLoger().info("查询文件"+fileName+"目录"+dir+"结果"+flag);
System.out.println("查询文件"+fileName+"目录"+dir+"结果"+flag);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
ftp.disconnect();
e.printStackTrace();
return false;
}
ftp.disconnect();
} catch (Exception e) {
return false;
}
return true;
}
下载展示 方法二:将图片转为base64码返回给前台,将会是一个超长的string~~
public String download(String path, String fileName) {
InputStream is = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ip, port);
ftpClient.login(loginName, pwd);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
path = replaceAll(path);
changeDirectory(path);
is = ftpClient.retrieveFileStream(fileName);// 获取远程ftp上指定文件的InputStream
if (null == is) {
throw new FileNotFoundException(fileName);
}
bos = new ByteArrayOutputStream();
int length;
byte[] buf = new byte[2048];
while (-1 != (length = is.read(buf, 0, buf.length))) {
bos.write(buf, 0, length);
}
ByteArrayInputStream fis = new ByteArrayInputStream(
bos.toByteArray());
bos.flush();
is.close();
bos.close();
byte[] buffer = new byte[fis.available()];
int offset = 0;
int numRead = 0;
while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
if (offset != buffer.length) {
throw new IOException("Could not completely read file ");
}
fis.close();
BASE64Encoder encoder = new BASE64Encoder();
imgStr = encoder.encode(buffer);
return imgStr; //超长的
} catch (Exception e) {
AppLogUtil.getAppLoger().error("ftp通过文件名称获取远程文件流", e);
} finally {
try {
close();
is.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
后台返回base64码的话,前台应该这么写:
<img src="https://img-blog.csdnimg.cn/2022010711075239025.jpeg"+imgStr> <!--前面的一段为比写 imgStr就是后台返回的base64码-->
FTPClient和Base64都需要jar包的,自己找吧 多的是~~
注:第一次写博客 难免会有疏漏 不懂的欢迎来问~~