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服务器文件上传临时路径处理问题的更多相关文章

  1. 简单将Springboot项目部署到linux服务器上

    1.使用springboot的jar包方式 直接使用maven工具按照步骤点击就可以直接打包 2.到target目录下找到 jar包 3.将jar包放到linux的任意文件夹下(此项目是之前的kafk ...

  2. 43-将javaweb项目部署到Linux服务器

    这是第二次弄了,感觉由于上次积累了点资源,这次要少走很多弯路了,再次记录下来吧. 第一次的记录:将本地的javaweb项目部署到Linux服务器的一般操作 1. 在Linux上建立数据库,我是将本地的 ...

  3. 上传文件到Ubuntu阿里云服务器(windows到Linux的文件上传)

    上传文件到Ubuntu阿里云服务器(windows到Linux的文件上传) 最近在阿里云上面租了一个轻量级服务器玩玩,学习学习怎么在服务器部署网站.然后嘞,在想要将本地文件上传到服务器的时候,自己研究 ...

  4. 前后端分离跨服务器文件上传-Java SpringMVC版

    近来工作上不上特别忙,加上对后台java了解一点,所以就抽时间,写了一个java版本的前后端分离的跨服务器文件上传功能,包括前后端代码. 一.Tomcat服务器部分 1.Tomcat服务器 单独复制一 ...

  5. Linux学习笔记(7)CRT实现windows与linux的文件上传下载

    Linux学习笔记(7)CRT实现windows与linux的文件上传下载 按下Alt + p 进入SFTP模式,或者右击选项卡进入 命令介绍 help 显示该FTP提供所有的命令 lcd 改变本地上 ...

  6. ASP.NET MVC 文件上传和路径处理

    ASP.NET MVC 文件上传和路径处理总结 目录 文件的上传和路径处理必须解决下面列出的实际问题: 1.重复文件处理 2.单独文件上传 3.编辑器中文件上传 4.处理文章中的图片路径 5.处理上传 ...

  7. springboot 定时任务部署至linux服务器上后会执行两次问题

    springboot定时任务在本地运行时,正常执行且只执行一次,但是在maven打包成war包,部署至linux服务器上之后,定时任务奇怪的执行了两次. 由于未做负载均衡,所以可以先排除是因为多台服务 ...

  8. ASP.NET项目部署到Linux服务器出现服务器错误

    在Linux系统中安装了Mono和Apache作为Web服务器,使用Visual Studio开发的ASP.NET Web应用或者API应用,在部署到Linux服务器后出现服务器错误,其中一个原因是由 ...

  9. mac通过自带的ssh连接Linux服务器并上传解压文件

    需求: 1:mac连接linux服务器 2:将mac上的文件上传到linux服务器指定位置 3:解压文件 mac上使用命令,推荐使用 iterm2 .当然,也可以使用mac自带的终端工具. 操作过程: ...

随机推荐

  1. IDEA中一个工程多个模块(module)分别提交到不同的git服务器

    说明:本文档适用于一个工程多个模块的项目,每个模块对应不同的git服务器地址.     一.将本地项目导入到git服务器   1.打开 File -> Settings,选择 Version C ...

  2. 使用 iframe + postMessage 实现跨域通信

    在实际项目开发中可能会碰到在 a.com 页面中嵌套 b.com 页面,这时第一反应是使用 iframe,但是产品又提出在 a.com 中操作,b.com 中进行显示,或者相反. 1.postMess ...

  3. .NET开源工作流RoadFlow-表单设计-隐藏域

    隐藏域即<input type="hidden" value=""/>标签:

  4. File GDB API

    bbs一问一答 https://blogs.esri.com/esri/arcgis/2010/12/13/file-geodatabase-api-details/ 下载页面 http://www. ...

  5. 任务六:通过HTML及CSS模拟报纸排版

    任务目的 深入掌握CSS中的字体.背景.颜色等属性的设置 进一步练习CSS布局 任务描述 参考 PDS设计稿(点击下载),实现页面开发,要求实现效果与 样例(点击查看) 基本一致 页面中的各字体大小, ...

  6. matlab练习程序(最大流/最小割)

    学习这个算法是为学习图像处理中的图割算法做准备的. 基本概念: 1.最大流是一个有向图. 2.一个流是最大流,当且仅当它的残余网络中不包括增广路径. 3.最小割就是网络中所有割中值最小的那个割,最小割 ...

  7. 订阅无法在 ARM 模式下创建虚拟机,只能在 ASM 模式下创建 Azure VM 部署

    问题描述 资源组所有者可以在新版 portal 创建经典模式的虚拟机,但是无法创建 ARM 模式的虚拟机. 问题现象 环境中有个相对权限比较高的账户,比如 account admin (以下简称为 A ...

  8. 一键完成SAP部署的秘密,想知道么?

    诸如 SAP 这样的企业级应用已成为普遍的流行趋势.考虑到不同行业和需求的特点,所选平台必须能够为不同层面用户和各种 IT 活动提供灵活的容量需求. 此时上云也许是种不错的选择,而想上云的企业,一方面 ...

  9. apache-实战(二)

    Apache 虚拟主机 --用apache或nginx就可以做 一台服务器跑多台web服务 VPS virtual private server 虚拟专用服务器 --使用虚拟化技术来做 云服务器 虚拟 ...

  10. 转一个csdn看到的帖子:而立之年的程序猿失业了 [问题点数:0分,结帖人jinxingfeng_cn]

    http://bbs.csdn.net/topics/390612263?page=1#post-395768948