2018-01-17 14:10:40 +08:00
|
|
|
package cn.keking.web.controller;
|
|
|
|
|
2018-01-19 14:51:18 +08:00
|
|
|
import cn.keking.service.FileConverQueueTask;
|
2018-01-17 17:51:53 +08:00
|
|
|
import cn.keking.service.FilePreview;
|
|
|
|
import cn.keking.service.FilePreviewFactory;
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2018-01-19 14:51:18 +08:00
|
|
|
import org.redisson.api.RBlockingQueue;
|
|
|
|
import org.redisson.api.RedissonClient;
|
2018-01-17 14:10:40 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.ui.Model;
|
2018-01-19 14:51:18 +08:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
2018-01-17 14:10:40 +08:00
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
2018-01-19 14:51:18 +08:00
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLConnection;
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author yudian-it
|
|
|
|
*/
|
|
|
|
@Controller
|
|
|
|
public class OnlinePreviewController {
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
@Autowired
|
|
|
|
FilePreviewFactory previewFactory;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
2018-01-19 14:51:18 +08:00
|
|
|
@Autowired
|
|
|
|
RedissonClient redissonClient;
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
/**
|
|
|
|
* @param url
|
|
|
|
* @param model
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
@RequestMapping(value = "onlinePreview", method = RequestMethod.GET)
|
2018-01-17 17:51:53 +08:00
|
|
|
public String onlinePreview(String url, Model model, HttpServletRequest req) {
|
2018-01-19 14:51:18 +08:00
|
|
|
req.setAttribute("fileKey", req.getParameter("fileKey"));
|
|
|
|
FilePreview filePreview = previewFactory.get(url);
|
|
|
|
return filePreview.filePreviewHandle(url, model);
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 多图片切换预览
|
|
|
|
*
|
|
|
|
* @param model
|
|
|
|
* @param req
|
|
|
|
* @return
|
|
|
|
* @throws UnsupportedEncodingException
|
|
|
|
*/
|
|
|
|
@RequestMapping(value = "picturesPreview", method = RequestMethod.GET)
|
2018-03-08 09:51:47 +08:00
|
|
|
public String picturesPreview(String urls, String currentUrl, Model model, HttpServletRequest req) throws UnsupportedEncodingException {
|
2018-01-17 14:10:40 +08:00
|
|
|
// 路径转码
|
|
|
|
String decodedUrl = URLDecoder.decode(urls, "utf-8");
|
2018-03-08 09:51:47 +08:00
|
|
|
String decodedCurrentUrl = URLDecoder.decode(currentUrl, "utf-8");
|
2018-01-17 14:10:40 +08:00
|
|
|
// 抽取文件并返回文件列表
|
2018-03-08 09:51:47 +08:00
|
|
|
String[] imgs = decodedUrl.split("\\|");
|
2018-01-17 14:10:40 +08:00
|
|
|
List imgurls = Arrays.asList(imgs);
|
|
|
|
model.addAttribute("imgurls", imgurls);
|
2018-03-08 09:51:47 +08:00
|
|
|
model.addAttribute("currentUrl",decodedCurrentUrl);
|
2018-01-17 14:10:40 +08:00
|
|
|
return "picture";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 根据url获取文件内容
|
|
|
|
* 当pdfjs读取存在跨域问题的文件时将通过此接口读取
|
|
|
|
*
|
|
|
|
* @param urlPath
|
|
|
|
* @param resp
|
|
|
|
*/
|
|
|
|
@RequestMapping(value = "/getCorsFile", method = RequestMethod.GET)
|
|
|
|
public void getCorsFile(String urlPath, HttpServletResponse resp) {
|
|
|
|
InputStream inputStream = null;
|
|
|
|
try {
|
|
|
|
String strUrl = urlPath.trim();
|
|
|
|
URL url = new URL(strUrl);
|
|
|
|
//打开请求连接
|
|
|
|
URLConnection connection = url.openConnection();
|
|
|
|
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
|
|
|
|
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
|
|
inputStream = httpURLConnection.getInputStream();
|
|
|
|
byte[] bs = new byte[1024];
|
|
|
|
int len;
|
|
|
|
while (-1 != (len = inputStream.read(bs))) {
|
|
|
|
resp.getOutputStream().write(bs, 0, len);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
if (inputStream != null) {
|
|
|
|
IOUtils.closeQuietly(inputStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:51:18 +08:00
|
|
|
/**
|
|
|
|
* 通过api接口入队
|
|
|
|
* @param url 请编码后在入队
|
|
|
|
*/
|
|
|
|
@GetMapping("/addTask")
|
|
|
|
@ResponseBody
|
|
|
|
public String addQueueTask(String url) {
|
|
|
|
final RBlockingQueue<String> queue = redissonClient.getBlockingQueue(FileConverQueueTask.queueTaskName);
|
|
|
|
queue.addAsync(url);
|
|
|
|
return "success";
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|