文件上传 Window & Linux
1. 在application配置文件添加图片存储路径的参数
上传路径前必须加 file:/ 否则网页图片请求可能404
window
gofy:
uploadPath: file:/F:/fileUpload/
imagesPath: F:/fileUpload/
imagesUrl: http://localhost:8888/images/
linux(在根目类home下的fileUpload文件夹):
gofy:
uploadPath: file:/home/fileUpload/
imagesPath: /home/fileUpload/
imagesUrl: http://域名/images/
2. 添加文件上传配置类
/**
* 上传配置类
* 图片放到/F:/fileUpload/后,从磁盘读取的图片数据scr将会变成images/picturename.jpg的格式
*/
@Configuration
public class UploadConfig implements WebMvcConfigurer { /**
* 在配置文件中配置的文件保存路径
*/
@Value("${gofy.uploadPath}")
private String uploadPath; @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if(uploadPath.equals("") || uploadPath.equals("${gofy.imagesPath}")){
String imagesPath = WebMvcConfig.class.getClassLoader().getResource("").getPath();
System.out.print("1.上传配置类imagesPath=="+imagesPath+"\n");
if(imagesPath.indexOf(".jar")>0){
imagesPath = imagesPath.substring(0, imagesPath.indexOf(".jar"));
}else if(imagesPath.indexOf("classes")>0){
imagesPath = "file:"+imagesPath.substring(0, imagesPath.indexOf("classes"));
}
imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/"))+"/images/";
uploadPath = imagesPath;
}
//System.out.print("imagesPath============="+mImagesPath+"\n");
//LoggerFactory.getLogger(WebAppConfig.class).info("imagesPath============="+mImagesPath+"\n");
registry.addResourceHandler("/images/**").addResourceLocations(uploadPath);
// TODO Auto-generated method stub
//System.out.print("2.上传配置类mImagesPath=="+mImagesPath+"\n"); WebMvcConfigurer.super.addResourceHandlers(registry);
}
}
3. 添加文件上传的工具类
数据库最好储存动态路径和绝对路径,动态路径用于项目使用,绝对路径便于文件删除
/**
* 文件上传工具类
*/
@Component
public class UploadUtil { @Value("${gofy.imagesPath}")
private String imagesPath;
@Value("${gofy.imagesUrl}")
private String imagesUrl;
/**
* 返回文件保存的路径
* @param file
* @return
*/
private String url;
public Map getFileUrl(MultipartFile file){
Map<String,String> map = new HashMap<String,String>();
System.out.println("上传文件===");
//判断文件是否为空
if(file.isEmpty()){
map.put("error","文件不能为空");
return map;
} //获取文件名
String fileName = file.getOriginalFilename();
System.out.println("上传的文件名为:"+fileName);
//加个时间戳,尽量避免文件名称重复
fileName= new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + fileName;
System.out.println("(加个时间戳,尽量避免文件名称重复)保存的文件名为: "+fileName); //文件绝对路径
String path = imagesPath+fileName;
System.out.println("保存文件的绝对路径"+path); //创建文件路径
File dest = new File(path); //判断文件父目类是否存在
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdir();
} try{
//上传文件
file.transferTo(dest);//保存文件
System.out.println("保存文件路径"+path);
url = imagesUrl+fileName;
System.out.println("保存的完整url==="+url);
}catch (IOException e){
e.printStackTrace();
map.put("error","上传失败");
return map;
}
map.put("url",url);
map.put("path",path);
return map;
}
}
我的个人博客
文件上传 Window & Linux的更多相关文章
- 《如何将windows上的软件包或文件上传到linux服务上》
昨天晚上朋友让我帮他简单的搭建个环境,他公司让他做款软件测试温度的,他自己搞的是嵌入式,在公司担任的是软件工程师,应届毕业生.也可能他们搞嵌入式的对这个linux系统不太熟,不会把windows上的软 ...
- 将Windows上的文件上传到Linux上
下载一个SSH Secure Shell Client即可. SSHSecureShellClient-3.2.9下载地址: 免费下载地址在 http://linux.linuxidc.com/ 用户 ...
- 通过XShell实现windows文件上传到Linux服务器上
.XShell上传文件到Linux服务器上 在学习Linux过程中,我们常常需要将本地文件上传到Linux主机上,这里简单记录下使用Xsheel工具进行文件传输 1:首先连接上一台Linux主机 2: ...
- 将windows下的文件上传到Linux服务器上
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/lx_Frolf/article/deta ...
- xshell如何将Windows文件上传到linux
1. 首先先将你xshell配置好用户名及密码等,必须使用有权限下载的账号进行操作. 使用 yum provides */rz 这条命令,查看你系统自带的软件包的信息. 2.在输出的信息中可 ...
- 中文名文件上传到linux服务器上以后文件名会乱码(openoffice)
1.中文名文件上传后保存在linux服务器上文件名会乱码,但是我们通过SSH直接对服务器上的一个文件进行重命名是可以使用中文的,而且显示出来是正确的,这说明服务器是可以支持中文的. 2.而为什么上传的 ...
- 本地文件上传到Linux服务器
1.从服务器上下载文件scp username@servername:/path/filename /var/www/local_dir(本地目录) 例如scp root@192.168.0.101: ...
- Jenkins使用-windows机器上的文件上传到linux
一.背景 最近的一个java项目,使用maven作包管理,通过jenkins把编译打包后war部署到另一台linux server上的glassfish(Ver3.1)中,在网上搜索的时候看到有人使用 ...
- RPC框架下实现文件上传到linux服务器
一.前端使用element -ui 的upload组件 :data 表示额外传递的参数, :header 表示设置请求头参数,如,token等, action表示后台api接口的地址 二. conto ...
随机推荐
- FCC成都社区·前端周刊 第 1 期
01. 2018 JavaScript 测试概览 文章介绍了JavaScript测试的关键术语.测试类型.工具和方法,并简要分析了工具jsdom.Istanbul.Karma.Chai.Wallaby ...
- 前端javascript知识(二)
documen.write和 innerHTML的区别 document.write只能重绘整个页面 innerHTML可以重绘页面的一部分 浏览器检测通过什么? (1) navigator.user ...
- proteus pro 8.9 安装及汉化教程
最近由于网上上课老师要求我们自己安装proteus这款仿真软件,所以笔者也安装了最新款版的proteus pro 8.9,分享给大家安装心得,也包含汉化过程,希望大家能用软件好好学习. 备注:感谢博主 ...
- axios下载文件乱码问题 无法解压 文件损坏
/* 下载附件 */ downloadFile(fileName) { // window.open(url); var that = this; var url = "PO2116&quo ...
- SpringBoot2 整合ElasticJob框架,定制化管理流程
本文源码:GitHub·点这里 || GitEE·点这里 一.ElasticJob简介 1.定时任务 在前面的文章中,说过QuartJob这个定时任务,被广泛应用的定时任务标准.但Quartz核心点在 ...
- 误删除所有redo日志的一组成员的处理过程
系统中共有3个日志文件组,每个组中各有一个日志文件成员.往系统中添加一个日志文件组,组中日志文件成员数量是2.SQL> alter database add logfile group 4 (' ...
- [pdo_mysql.lo] Error 1 或者 [php_mysql.lo] Error 1
make: *** [pdo_mysql.lo] Error 1 make: *** [php_mysql.lo] Error 1 这是因为这是因为在编译时需要 MySQL 的头的文件.而它按默认搜索 ...
- 2020centos解决“nginx 403 Forbidden"错误的故事
最近折腾一个放在日本的vps,网速还可以,就是经常丢包. 原本配置了Nginx的做代理服务器,我想反正服务器空闲者,放点我自己的资料 配置了一个静态html文件,方便自己随时查看 结果,不停的修改ng ...
- openwrt 外挂usb 网卡 RTL8188CU 及添加 RT5572 kernel支持
RT5572 原来叫 Ralink雷凌 现在被 MTK 收购了,淘宝上买的很便宜50块邮,2.4 5G 双频.在 win10 上插了试试,果然是支持 5G.这上面写着 飞荣 是什么牌子,有知道的和我说 ...
- springboot项目中thymeleaf布局应用
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...