网易云NOS产生的跨域问题
查看文件
可以看到成功显示的pdf文件的Content-Type两者是不一样的,所以就有了方案一,通过后台去下载一次文件,再将Content-Type进行修改,再返回给前端进行展示
方案一:
通过后台接口进行中间转发处理
import com.alibaba.fastjson.JSON;
import com.zj.annotation.SwaggerVisible;
import com.zj.api.entity.common.request.ResultEnum;
import com.zj.api.entity.common.response.ResultUtil;
import com.zj.api.util.UrlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.BufferedInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
@GetMapping(value="/single/file")
public void downloadSingleFile(
@RequestParam(value="url",required=true) String url,
ServletResponse response
) throws Exception{
if(url == null || url.isEmpty()) {
String result = JSON.toJSONString(ResultUtil.exception(ResultEnum.ERROR_NO_FILE));//ERROR_NO_FILE(614, "文件不存在!")
response.setContentType("text/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(result);
return;
}
try {
// 1、根据url判断文件格式
String contentType = UrlUtil.getContentTypeByUrl(url);
if(contentType == null){
contentType = "application/octet-stream";
}
response.setContentType(contentType);
// 2、判断url地址是否能够成功访问
URL u = new URL(url);
URLConnection urlConnection = u.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.connect();
BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
int count = 0;
// 3、写入response 返回
byte[] buf = new byte[2048];
while ((count = bin.read(buf)) != -1) {
response.getOutputStream().write(buf,0,count);
}
bin.close();
response.flushBuffer();
return;
}catch (Exception e){
String result = JSON.toJSONString(ResultUtil.exception(ResultEnum.SERVER_ERROR));//SERVER_ERROR(501, "服务端异常")
response.setContentType("text/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(result);
return;
}
}
文件content-type 配置
/**
* @describe: 文件content-type 配置
* Create by Zheng
*/
public final class UrlUtil {
private static HashMap<String,String> contentMap;
static{
contentMap = new HashMap<>();
//通过单独配置文件来存储url文件名后缀与content-type的对应关系
contentMap.put("pdf","application/pdf");
contentMap.put("png","image/png");
contentMap.put("txt","text/plain");
contentMap.put("ppt","application/x-ppt");
contentMap.put("pptx","application/x-ppt");
contentMap.put("doc","application/msword");
contentMap.put("docx","application/msword");
contentMap.put("mp4","video/mp4");
contentMap.put("png","video/x-flv");
}
public static String getContentTypeByUrl(String url){
String ext = getExtraOfUrl(url);
if(ext == null || ext.isEmpty()){
return null;
}
if(!contentMap.containsKey(ext)){
return null;
}
return contentMap.get(ext);
}
public static String getExtraOfUrl(String url){
if(url == null || url.isEmpty()){
return null;
}
int index = url.lastIndexOf('.');
if(index < 0)
return null;
return url.substring(index + 1, url.length());
}
}
最后通过接口请求带来的url参数进行获取文件