|
|
@@ -8,6 +8,7 @@ import java.io.OutputStream;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
+import com.uas.dfs.service.FileClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
@@ -29,6 +30,9 @@ public class FileController {
|
|
|
@Autowired
|
|
|
private AttachService attachService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private FileClient fileClient;
|
|
|
+
|
|
|
/**
|
|
|
* 文件下载
|
|
|
*
|
|
|
@@ -38,20 +42,36 @@ public class FileController {
|
|
|
*/
|
|
|
@RequestMapping("/{fileId}")
|
|
|
public void download(@PathVariable("fileId") long fileId, HttpServletResponse response) throws IOException {
|
|
|
- Attach attch = attachService.getAttach(fileId);
|
|
|
- File file = new File(attch.getPath());
|
|
|
- InputStream in = new FileInputStream(file);
|
|
|
- OutputStream os = response.getOutputStream();
|
|
|
- response.addHeader("Content-Disposition", "attachment;filename=" + new String(attch.getName().getBytes("utf-8"), "iso-8859-1"));
|
|
|
- response.addHeader("Content-Length", String.valueOf(file.length()));
|
|
|
+ Attach attach = attachService.getAttach(fileId);
|
|
|
+ if(attach == null) throw new IllegalArgumentException("附件不存在");
|
|
|
+ response.addHeader("Content-Disposition", "attachment;filename=" + new String(attach.getName().getBytes("utf-8"), "iso-8859-1"));
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
response.setContentType("application/octec-stream");
|
|
|
- int data = 0;
|
|
|
- while ((data = in.read()) != -1) {
|
|
|
- os.write(data);
|
|
|
+ String path = attach.getPath();
|
|
|
+ if(path.startsWith("http://dfs.ubtob.com")) {// 存储在dfs存储服务器,去dfs存储服务器下载
|
|
|
+ byte[] fileBytes = fileClient.download(attach.getPath());
|
|
|
+ if (fileBytes.length <= 0) throw new IllegalArgumentException("附件不存在");
|
|
|
+ response.addHeader("Content-Length", String.valueOf(fileBytes.length));
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
+ os.write(fileBytes);
|
|
|
+ os.close();
|
|
|
+ } else if(path.startsWith("http://") || path.startsWith("https://") || path.startsWith("ftp://") || path.startsWith("sftp://")) {
|
|
|
+ // 存放在其他网络资源中,直接跳转至链接地址
|
|
|
+ response.sendRedirect(path);
|
|
|
+ } else {
|
|
|
+ // 其他的当做是存放在本地服务器上,去本地服务器根据文件路径去获取
|
|
|
+ File file = new File(attach.getPath());
|
|
|
+ if(!file.exists()) throw new IllegalArgumentException("附件不存在");
|
|
|
+ response.addHeader("Content-Length", String.valueOf(file.length()));
|
|
|
+ InputStream in = new FileInputStream(file);
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
+ int data = 0;
|
|
|
+ while ((data = in.read()) != -1) {
|
|
|
+ os.write(data);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ os.close();
|
|
|
}
|
|
|
- in.close();
|
|
|
- os.close();
|
|
|
}
|
|
|
|
|
|
}
|