没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
目录
1、文件的上传下载本地 .................................................................................................................................1
1.1、上传图片............................................................................................................................................2
1.2、上传文件............................................................................................................................................2
1.3、文件下载(仅用于 windows,且只能用浏览器运行).........................................................4
1.4、文件下载(通用) .........................................................................................................................6
1.5、多文件的上传 ..................................................................................................................................7
2、文件的上传下载到远程服务器(推荐).............................................................................................8
2.1、安装 ftp 组件,配置 ftp 服务器(centos7)............................................................................8
2.1.1、安装 yum...............................................................................................................................8
2.1.2、通过 yum 安装 ftp..............................................................................................................8
2.1.3、修改配置文件(匿名用户模式) .................................................................................8
2.1.4、修改配置文件(系统用户模式)...............................................................................11
2.2 搭建 nginx 文件服务器(centos7)..........................................................................................14
2.2.1、下载源码(linux 环境的只有通过源码安装。windows 环境的直接解压就可
以使用) ..........................................................................................................................................14
2.2.2、安装编译环境....................................................................................................................15
2.2.3、安装 nginx..........................................................................................................................15
2.2.4、启动 nginx..........................................................................................................................17
2.2.5、测试......................................................................................................................................18
2.2.6、修改/usr/local/nginx/conf/nginx.conf.......................................................................18
2.2.7、每次打开虚拟机,启动 nginx 的命令.......................................................................21
2.3、java 代码(springboot)...........................................................................................................22
2.3.1、添加依赖.............................................................................................................................22
2.3.1、application. properties ...................................................................................................22
2.3.2、实体类 FtpConfig............................................................................................................23
2.3.3、上传的工具类 FtpUtil......................................................................................................25
2.3.4、工具类 UploadUtils.java.................................................................................................30
2.3.5、controller 层 ......................................................................................................................31
2.3.6、测试......................................................................................................................................34
3、fastdfs 分布式文件系统..........................................................................................................................34
3.1、分布式文件系统 FastDFS 详解.................................................................................................34
3.2、FastDFS 分布式文件系统集群安装与配置...........................................................................34
1、文件的上传下载本地
具体解释一下“本地”即前提:项目和文件服务器(nginx)需一起部署到本地(win10)或者
一起部署到服务器上(linux)
1.1、上传图片
//上传头像
@PostMapping("/headImg")
public Object uploadHeadimg(@RequestParam("file") MultipartFile file) {
String username =
SecurityContextHolder.
getContext
().getAuthentication().getName();//获取当前
登录用户
System.
out
.println( username );
User user = userService.getUserByUsername(username);
if (!file.isEmpty()) { //文件不是空文件
try {
BufferedOutputStream out = new BufferedOutputStream(
//C:\IDEA_mode_project\agriculture\src\main
new FileOutputStream(new File(filepath + username +
".jpg")));//保存图片到目录下,建立保存文件的输入流
out.write(file.getBytes());
out.flush();
out.close();
String filename = filepath + username + ".jpg";
user.setAvater(filename); //设置头像路径
userService.saveOrUpdate(user);//修改用户信息
} catch (FileNotFoundException e) {
e.printStackTrace();
return new Reponse(false,"上传失败," + e.getMessage());
//return "上传失败," + e.getMessage(); //文件路径错误
} catch (IOException e) {
e.printStackTrace();
return new Reponse(false,"上传失败," + e.getMessage());
//return "上传失败," + e.getMessage(); //文件 IO 错误
}
return new Reponse(true,"上传头像成功",user);//返回用户信息
} else {
return new Reponse(false,"上传失败,因为文件是空的");
}
}
1.2、上传文件
可以参考:https://juejin.im/post/5a5dddde5188257328216ef4
@PostMapping("/docUpload")
public Object docUpload(@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam("file") MultipartFile file ) {
String author =
SecurityContextHolder.
getContext
().getAuthentication().getName();//获取当前
登录用户
User user = userService.getUserByUsername( author );
//System.out.println( user );
String fileName = file.getOriginalFilename().toString();//获取文件名
//System.out.println( fileName );
if(fileName.indexOf('?')!=fileName.length()-1)
fileName=title+fileName.substring(fileName.lastIndexOf("."));
final SimpleDateFormat sDateFormate = new
SimpleDateFormat("yyyymmddHHmmss"); //设置时间格式
String nowTimeStr = sDateFormate.format(new Date()); // 当前时间
fileName=fileName.substring(0,fileName.indexOf("."))+nowTimeStr+fileName.sub
string(fileName.lastIndexOf("."));
Doc doc = new Doc();
if (!file.isEmpty()) {
try {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(filepath + fileName)));//
保存图片到目录下,建立保存文件的输入流
out.write(file.getBytes());
out.flush();
out.close();
String filename = filepath+fileName;
Long fileSize = file.getSize();
System.
out
.println( file.getSize());
doc.setTitle( title );
doc.setAvatar( filename );
doc.setAuthor( author );
doc.setAuthor_picture(user.getAvater());
doc.setUptime( new Date() );
doc.setDescription( description );
doc.setFileSize( fileSize );
docService.saveOrUpdateDoc( doc );
} catch (FileNotFoundException e) {
e.printStackTrace();
return new Reponse(false,"上传文件失败," + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
return new Reponse(false,"上传文件失败," + e.getMessage());
}
return new Reponse(true,"上传文件成功",doc);//返回文件信息
}
else {
return new Reponse(false,"上传失败,因为文件是空的");
}
}
1.3、文件下载(仅用于 windows,且只能用浏览器运行)
/**
* Author: zj
*
* 文件下载(仅用于 windows,且只能用浏览器运行)
*
* 如果不设置,则默认下载到 C:\\users\\downloads,即本机的默认下载的目录
* @param id
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/download",method = RequestMethod.
GET
)
public Object downloadFile(@RequestParam("id") Long id,
org.apache.catalina.servlet4preview.http.HttpServletRequest request,
HttpServletResponse response){
Doc doc = docService.getDocById( id );
String fileName = doc.getAvatar();//获取文件路径
System.
out
.println( fileName );
String filename = fileName.split( "/")[4];//获取文件名,该数字要具体
情况修改
System.
out
.println( filename );
//String downPath = "C:\\Users\\zj\\Desktop\\"+filename;
if (fileName != null) {
File file = new File(fileName);
System.
out
.println( file );
if (file.exists()) {
response.setContentType("application/force-download");// 设
置强制下载不打开
response.addHeader("Content-Disposition",
"attachment;fileName=" + filename);// 设置文件名
// response.addHeader("Content-Disposition",
// "attachment;filePath=" + downPath);// 设置文件路
径
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);//下载的源文件流
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
//System.out.println("success");
Integer downloads = doc.getDownloads();
doc.setDownloads( downloads+1 );
docService.saveOrUpdateDoc( doc );
dataService.editDocData();
return new Reponse( true,"文件下载成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
剩余34页未读,继续阅读
袁大岛
- 粉丝: 31
- 资源: 305
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0