Commit 0e4a9f29 by 水库浪子

文件下载相关代码提交;

1 parent 98003001
...@@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory; ...@@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; 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.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -33,9 +34,6 @@ public class CommonController { ...@@ -33,9 +34,6 @@ public class CommonController {
@Autowired @Autowired
private ServerConfig serverConfig; private ServerConfig serverConfig;
@Autowired
private CosConfig cosConfig;
@Autowired @Autowired
private FileService fileService; private FileService fileService;
...@@ -122,12 +120,8 @@ public class CommonController { ...@@ -122,12 +120,8 @@ public class CommonController {
@PostMapping("/common/uploadFile2Cos") @PostMapping("/common/uploadFile2Cos")
public AjaxResult uploadFile2Cos(MultipartFile file) throws Exception { public AjaxResult uploadFile2Cos(MultipartFile file) throws Exception {
try { try {
SysFile sysFile SysFile sysFile
= fileService.upload(file); = fileService.upload(file);
// String fileName = FileUploadUtils.upload2Cos(file, cosConfig);
AjaxResult ajax = AjaxResult.success(); AjaxResult ajax = AjaxResult.success();
ajax.put("fileName", sysFile.getFileName()); ajax.put("fileName", sysFile.getFileName());
ajax.put("url", sysFile.getImgUrlPrefix()+sysFile.getFileName()); ajax.put("url", sysFile.getImgUrlPrefix()+sysFile.getFileName());
...@@ -138,8 +132,15 @@ public class CommonController { ...@@ -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; ...@@ -9,8 +9,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
/** /**
...@@ -37,7 +40,7 @@ public class FileService { ...@@ -37,7 +40,7 @@ public class FileService {
* @return * @return
* @throws IOException * @throws IOException
*/ */
public SysFile upload(MultipartFile file) throws IOException { public SysFile upload(MultipartFile file) throws IOException {
String fileName=""; String fileName="";
if(ruoYiConfig.getFile().isStartCosUpload()){//开启云上传 if(ruoYiConfig.getFile().isStartCosUpload()){//开启云上传
//开启云上传开关则云上传,不然上传本地 //开启云上传开关则云上传,不然上传本地
...@@ -56,4 +59,40 @@ public class FileService { ...@@ -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
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!