网易云NOS文件查看跨域问题解决方案

网易云NOS产生的跨域问题
在这里插入图片描述
查看文件
加载失败的图片

加载成功的pdf
可以看到成功显示的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参数进行获取文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值