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;
}*/
}
}