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自带的终端工具. 操作过程: ...
随机推荐
- BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...
- js 标签属性与导航
导航标签的方法: 一 , 全局导航: 1.通过by id导航 <!DOCTYPE html><html lang="en"><head> &l ...
- bzoj 5084: hashit
Description 你有一个字符串S,一开始为空串,要求支持两种操作 在S后面加入字母C 删除S最后一个字母 问每次操作后S有多少个两两不同的连续子串 Solution 先忽略删除操作,建出最终的 ...
- ubuntu遇到了 dpkg was interrupted, you must manually run 'dpkg..的问题
dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. E: _cache- ...
- 查看mysql版本的四种方法及常用命令
1:在终端下:mysql -V或mysql -Version. 以下是代码片段: [shengting@login ~]$ mysql -V mysql Ver 14.7 Distrib 4.1.10 ...
- 操作系统-Interrupts
- Bash 脚本语法
每次学了忘,忘了学,怎么记不住,因为长时间不用了 Bash 流程控制 循环 for循环 for item in $list do echo $item done 另一种与C语言类似的写法 ; i< ...
- Django——CBV与FBV
一.FBV FBV(function base views) 就是在视图里使用函数处理请求. 二.CBV CBV(class base views) 就是在视图里使用类处理请求. Python是一个面 ...
- 网易回合制游戏录像批量下载(失效 不是因为代码 是因为网易官方关闭了录像网站 :P)
最近在访问网易大话西游2的录像专区时,发现页面还是很早之前的板式,网易的编辑并没有打算重新美化的打算,不由得内心一寒,结合之前好几个回合制游戏的倒闭,让很多人回顾都没办法回顾, 而且,很多人现在也没有 ...
- 【Linux】TFTP & NFS 服务器配置
Why?--交叉开发 一.交叉开发模型 宿主机(PC)------ 网络.串口.USB.JTAG ------ 目标机(ARM系统) PC机作为TFTP & NFS 服务器,目标机从网络下载软 ...