|
|
@@ -13,6 +13,7 @@ import java.net.URLConnection;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
+import org.junit.Test;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Scope;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
@@ -141,50 +142,28 @@ public class PublicController extends BaseController {
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
- @RequestMapping(value = "/downLoadPicture", method = RequestMethod.POST)
|
|
|
- public void downLoadPicture(HttpServletResponse response, String path, HttpServletRequest request,
|
|
|
- boolean isPrivate) {
|
|
|
- String filename = (path.split("/")[(path.split("/").length - 1)]).split(".")[0];
|
|
|
- response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
|
|
|
-
|
|
|
- // 设置文件MIME类型
|
|
|
- response.setContentType("multipart/form-data");
|
|
|
- /*
|
|
|
- * 解决各浏览器的中文乱码问题
|
|
|
- */
|
|
|
- String userAgent = request.getHeader("User-Agent");
|
|
|
+ @RequestMapping(value = "/downLoadPicture", method = RequestMethod.GET)
|
|
|
+ public void downLoadPicture(HttpServletResponse response, String path, HttpServletRequest request
|
|
|
+ ) {
|
|
|
+ //String filename = (path.split("/")[(path.split("/").length - 1)]);
|
|
|
+ String filename =Long.toString(System.nanoTime())+".jpg";
|
|
|
InputStream in = null;
|
|
|
OutputStream out = null;
|
|
|
- int connectTimeout = 30 * 1000; // 连接超时:30s
|
|
|
- int readTimeout = 1 * 1000 * 1000; // IO超时:1min
|
|
|
- byte[] buffer = new byte[8 * 1024]; // IO缓冲区:8KB
|
|
|
+ byte[] buffer = new byte[8 * 1024];
|
|
|
String fileSaveRootPath = "";
|
|
|
-
|
|
|
+ //下载权限判断
|
|
|
+
|
|
|
try {
|
|
|
-
|
|
|
- if (isPrivate) {
|
|
|
fileSaveRootPath = uploadPrivatePath + path;
|
|
|
File file = new File(fileSaveRootPath);
|
|
|
in = new FileInputStream(file);
|
|
|
- } else {
|
|
|
- fileSaveRootPath =" http://"+ staticHost + "upload" + path;
|
|
|
- URL url = new URL(fileSaveRootPath);
|
|
|
- URLConnection conn = url.openConnection();
|
|
|
- conn.setConnectTimeout(connectTimeout);
|
|
|
- conn.setReadTimeout(readTimeout);
|
|
|
- conn.connect();
|
|
|
- in = conn.getInputStream();
|
|
|
- }
|
|
|
out = response.getOutputStream();
|
|
|
// 设置文件MIME类型
|
|
|
- response.setContentType("multipart/form-data");
|
|
|
- /*
|
|
|
- * 解决各浏览器的中文乱码问题
|
|
|
- */
|
|
|
- userAgent = request.getHeader("User-Agent");
|
|
|
- byte[] bytes1 = userAgent.contains("MSIE") ? filename.getBytes() : filename.getBytes("UTF-8"); // fileName.getBytes("UTF-8")处理safari的乱码问题
|
|
|
- filename = new String(bytes1, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
|
|
|
- response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
|
|
|
+ //response.setContentType("multipart/form-data");
|
|
|
+ //response.setContentType("application/x-msdownload");
|
|
|
+ //response.setContentType("application/x-jpg");
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=" + filename);
|
|
|
for (;;) {
|
|
|
int bytes = in.read(buffer);
|
|
|
if (bytes == -1) {
|
|
|
@@ -192,10 +171,6 @@ public class PublicController extends BaseController {
|
|
|
}
|
|
|
out.write(buffer, 0, bytes);
|
|
|
}
|
|
|
- } catch (MalformedURLException e) {
|
|
|
- LoggerUtils.fmtError(getClass(), e, "URL协议、格式或者路径错误:%s", e.getMessage());
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- LoggerUtils.fmtError(getClass(), e, "文件未找到:%s", e.getMessage());
|
|
|
} catch (IOException e) {
|
|
|
LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
|
|
|
} finally {
|
|
|
@@ -223,5 +198,6 @@ public class PublicController extends BaseController {
|
|
|
return file.delete();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
|
|
|
}
|