20.springboot项目部署到linux服务器文件上传临时路径处理问题
1.前言
把项目部署到服务器上之后,文件上传默认会在/tmp路径中。
之前想了各种解决办法,比如如何更改这个上传路径。。。。。。
最后发现不是个好的方法,当然就想到了更好的解决方案。
就是我把上传文件存储到临时路径里,我在通过File类的文件移动方法移动到我想要的路径下,就解决了这个问题。
2.解决方案
package com.xm.zeronews.controller;
import com.xm.zeronews.pojo.User;
import com.xm.zeronews.service.UserService;
import com.xm.zeronews.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
import java.util.UUID;
/**
* 作者:Xm Guo
* 时间:2018/11/15
**/
@Api(value="FileController",tags="文件上传管理")
@RestController
@CrossOrigin
@RequestMapping("/upload")
public class FileController {
@Autowired
private UserService userService;
private Long MaxSize;
@Value("${fileUpload.path}")
private String path;
@Value("${fileUpload.file}")
private String filepaths;
@ApiOperation(value="上传头像")
@PostMapping("/head")
public String uploadHead(MultipartFile file) {
String filename = file.getOriginalFilename();
return upload(file,false,"head"+filename.substring(filename.lastIndexOf('.')));
}
@ApiOperation(value="上传背景")
@PostMapping("/bg")
public String uploadBg(MultipartFile file) {
String filename = file.getOriginalFilename();
filename = upload(file,false,"bg"+filename.substring(filename.lastIndexOf('.')));
User user = new User();
user.setId(UserUtil.getUserId());
user.setBg(filename);
userService.updateById(user);
return filename;
}
@ApiOperation(value="上传新闻图片")
@PostMapping("/news")
public String uploadNews(MultipartFile file) {
String filename= null;
try{
filename= file.getOriginalFilename();
} catch(Exception e) {
e.printStackTrace();
}
filename = UUID.randomUUID().toString().replace("-", "").toLowerCase() + filename.substring(filename.lastIndexOf('.'));
return upload(file,true,filename);
}
private String upload(MultipartFile file,Boolean isNews,String fileName) {
File tmpfile = new File("/"+fileName);
try {
file.transferTo(tmpfile);
} catch (IOException e) {
e.printStackTrace();
}
String filepath = filepaths;
filepath += UserUtil.getUserId();
File upFile = new File(path+filepath);
if(!upFile.exists()) {
upFile.mkdir();
}
if(isNews) {
filepath += "/news";
upFile = new File(path+filepath);
if(!upFile.exists()) {
upFile.mkdir();
}
}
filepath += "/"+ fileName;
upFile = new File(path,filepath);
System.out.println("文件上传路径:"+upFile.getAbsolutePath());
if(tmpfile.renameTo(upFile)){
System.out.println("文件上传成功!");
return filepath;
} else {
System.out.println("文件上传失败!");
}
return null;
/*FileChannel inChannel =null;
FileChannel outChannel = null;
try {
inChannel =FileChannel.open(tmpfile.toPath(), StandardOpenOption.READ);
*//**
* StandardOpenOption.CREATE与StandardOpenOption.CREATE_NEW的区别
* 1.StandardOpenOption.CREATE:无则创建,有则覆盖
* 2.StandardOpenOption.CREATE_NEW:无则创建,有则报错
*//*
outChannel =FileChannel.open(upFile.toPath(), StandardOpenOption.WRITE,StandardOpenOption.CREATE);
//3.定义缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
//4.读取数据到缓冲区,再从缓冲区写入到文件
while(inChannel.read(buffer) != -1) {
//切换到读模式
buffer.flip();
//写操作到管道
outChannel.write(buffer);
//清空buffer
buffer.clear();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//5.关闭通道和流
if(inChannel != null) {
try {
inChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(outChannel != null) {
try {
outChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return filepath;
}*/
}
}
20.springboot项目部署到linux服务器文件上传临时路径处理问题的更多相关文章
- 简单将Springboot项目部署到linux服务器上
1.使用springboot的jar包方式 直接使用maven工具按照步骤点击就可以直接打包 2.到target目录下找到 jar包 3.将jar包放到linux的任意文件夹下(此项目是之前的kafk ...
- 43-将javaweb项目部署到Linux服务器
这是第二次弄了,感觉由于上次积累了点资源,这次要少走很多弯路了,再次记录下来吧. 第一次的记录:将本地的javaweb项目部署到Linux服务器的一般操作 1. 在Linux上建立数据库,我是将本地的 ...
- 上传文件到Ubuntu阿里云服务器(windows到Linux的文件上传)
上传文件到Ubuntu阿里云服务器(windows到Linux的文件上传) 最近在阿里云上面租了一个轻量级服务器玩玩,学习学习怎么在服务器部署网站.然后嘞,在想要将本地文件上传到服务器的时候,自己研究 ...
- 前后端分离跨服务器文件上传-Java SpringMVC版
近来工作上不上特别忙,加上对后台java了解一点,所以就抽时间,写了一个java版本的前后端分离的跨服务器文件上传功能,包括前后端代码. 一.Tomcat服务器部分 1.Tomcat服务器 单独复制一 ...
- Linux学习笔记(7)CRT实现windows与linux的文件上传下载
Linux学习笔记(7)CRT实现windows与linux的文件上传下载 按下Alt + p 进入SFTP模式,或者右击选项卡进入 命令介绍 help 显示该FTP提供所有的命令 lcd 改变本地上 ...
- ASP.NET MVC 文件上传和路径处理
ASP.NET MVC 文件上传和路径处理总结 目录 文件的上传和路径处理必须解决下面列出的实际问题: 1.重复文件处理 2.单独文件上传 3.编辑器中文件上传 4.处理文章中的图片路径 5.处理上传 ...
- springboot 定时任务部署至linux服务器上后会执行两次问题
springboot定时任务在本地运行时,正常执行且只执行一次,但是在maven打包成war包,部署至linux服务器上之后,定时任务奇怪的执行了两次. 由于未做负载均衡,所以可以先排除是因为多台服务 ...
- ASP.NET项目部署到Linux服务器出现服务器错误
在Linux系统中安装了Mono和Apache作为Web服务器,使用Visual Studio开发的ASP.NET Web应用或者API应用,在部署到Linux服务器后出现服务器错误,其中一个原因是由 ...
- mac通过自带的ssh连接Linux服务器并上传解压文件
需求: 1:mac连接linux服务器 2:将mac上的文件上传到linux服务器指定位置 3:解压文件 mac上使用命令,推荐使用 iterm2 .当然,也可以使用mac自带的终端工具. 操作过程: ...
随机推荐
- Windows phone 8.1应用集成cortana语音命令
微软推出小娜已经有一段时间了,最近恰好在研究其用法,就随便写点记录一下自己的心得. 在研究时参考了@王博_Nick的博客:http://www.cnblogs.com/sonic1abc/p/3868 ...
- !function()是干什么的?
叹号后面跟函数!function和加号后面跟函数+function都是跟(function(){})();这个函数是一个意思,都是告诉浏览器自动运行这个匿名函数的,因为!+()这些符号的运算符是最高的 ...
- vbScript: 编号成生不夠位數前面加零
'编号成生Geovin Du '不夠位數前面加零 塗聚文 Function getStringlen(str,lenint) so="" itop=0 slen=Len(str) ...
- dataBinding与ListView及事件
2015年Google IO大会分布了DataBinding库,能够更快捷便利的实现MVVM结构模式.但是,通过对DataBinding的学习,其中踩过得坑,今天要在这里记录一下.对于DataBind ...
- 16_AOP入门准备_Jdk动态代理模式
[工程截图] [PersonDao.java] package com.HigginCui.daoProxy; //目标类接口 public interface PersonDao { public ...
- Azure 10月新公布
Azure 10月新发布:F 系列计算优化实例,认知服务,媒体服务流式处理单元更名,Azure 镜像市场,FreeBSD 适用于Azure 虚拟机的全新 F 系列计算优化实例 Azure 虚拟机的全新 ...
- 【Leetcode】【Easy】Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- # putty的使用和保存配置
putty的使用和保存配置 之前使用的xshell5,但是突然之间需要我去注册,根本无法使用.在网上看到可以到官网申请家庭和学校版本,但是我的邮箱一直没有接收到邮件.于是我放弃xshell.就拿起了之 ...
- sql developer中英文切换
今天使用oracle sql developer时做调优建议时找到的建议显示为?的乱码,本人sql developer为中文版,修改为英文版后问题解决. 查看帮助菜单中的属性选项卡,user.lang ...
- C++ decltype类型说明符(尾置返回类型使用)
转自https://blog.csdn.net/yhl_leo/article/details/50865552 1 基本语法 decltype 类型说明符生成指定表达式的类型.在此过程中,编译器分析 ...