提交 a102c340 编写于 作者: 水库浪子

文件下载相关代码提交;

上级 36429072
......@@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
......@@ -33,9 +34,6 @@ public class CommonController {
@Autowired
private ServerConfig serverConfig;
@Autowired
private CosConfig cosConfig;
@Autowired
private FileService fileService;
......@@ -122,12 +120,8 @@ public class CommonController {
@PostMapping("/common/uploadFile2Cos")
public AjaxResult uploadFile2Cos(MultipartFile file) throws Exception {
try {
SysFile sysFile
= fileService.upload(file);
// String fileName = FileUploadUtils.upload2Cos(file, cosConfig);
AjaxResult ajax = AjaxResult.success();
ajax.put("fileName", sysFile.getFileName());
ajax.put("url", sysFile.getImgUrlPrefix()+sysFile.getFileName());
......@@ -138,8 +132,15 @@ public class CommonController {
}
// public String url(String fileName){
// return "https://link-wechat-1251309172.cos.ap-nanjing.myqcloud.com/"+fileName;
// }
/**
* 本地图片下载
*/
@GetMapping("/findFile")
public void findFile(HttpServletResponse response,String fileName){
fileService.findFile(fileName,response);
}
}
......@@ -9,8 +9,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
......@@ -37,7 +40,7 @@ public class FileService {
* @return
* @throws IOException
*/
public SysFile upload(MultipartFile file) throws IOException {
public SysFile upload(MultipartFile file) throws IOException {
String fileName="";
if(ruoYiConfig.getFile().isStartCosUpload()){//开启云上传
//开启云上传开关则云上传,不然上传本地
......@@ -56,4 +59,40 @@ public class FileService {
}
public void findFile(String fileName, HttpServletResponse rp){
String fileDownUrl="";
// if(ruoYiConfig.getFile().isStartCosUpload()) {//开启云上传
// fileDownUrl= ruoYiConfig.getFile().getImgUrlPrefix();
// }else{
fileDownUrl=OsUtils.isWindows()?WINDOWSFILEPATH+fileName:LINUXFILEPATH+fileName;
// }
File file=new File(fileDownUrl);
if (file.exists()) {
FileInputStream fis = null;
OutputStream os = null;
try {
fis = new FileInputStream(file);
os = rp.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024 * 8];
while ((count = fis.read(buffer)) != -1) {
os.write(buffer, 0, count);
os.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册