js-tus-client + springboot + go-fastdfs文件服务器 转发tus请求到go-fastdfs服务器

注意:要引入hutool包
go-fastdfs的分片上传、断点续传url为:http://10.1.5.9:8080//big/upload/
请求的链接要与go-fastdfs的链接一样,
如:go-fastdfs:http://10.1.5.9:8080/group1/big/upload/,
那么请求链接为:http://192.168.0.100:8888/group1/big/upload/

其他的tus服务器应该也适用(猜测),只是链接不一样
go-fastdfs文档地址:问题汇总 · GitBook (sjqzhang.github.io)
转发参考地址:HttpServletRequest的转发到另一台服务器(Forward HttpServletRe - 码农岛 (manongdao.com)

/**
 * 注意:前端要使用js-tus-client
 */
@RestController
@Slf4j
public class TusForwardAction extends BaseAction {

    private String goFastDfsUrl;
    private String group;

    /**
     * 获取tus上传链接
     * @param request
     * @param response
     */
    @PostMapping(value = "group1/big/upload")
    public void tusForward(HttpServletRequest request, HttpServletResponse response) {
        HttpRequest post = HttpRequest.post(goFastDfsUrl + "/" + group + "/big/upload/");
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            String name = request.getHeader(key);
            log.info("post request header: {} - {}", key, name);
            post.header(key, name);
        }
        HttpResponse execute = post.execute();
        for (Map.Entry> entry : execute.headers().entrySet()) {
            if ("Access-Control-Allow-Origin".equals(entry.getKey())) {
                response.setHeader("Access-Control-Allow-Origin", "*");
                continue;
            }
            String json = String.join(",", entry.getValue());
            log.info("post response header: {} - {}", entry.getKey(), json);
            response.setHeader(entry.getKey(), json);
        }
    }

    /**
     * 获取offset
     * @param request
     * @param response
     * @param secretKey
     */
    @RequestMapping(value = "group1/big/upload/{secretKey}", method = RequestMethod.HEAD)
    public void tusForward4Head(HttpServletRequest request, HttpServletResponse response, @PathVariable String secretKey) {
        HttpRequest patch = HttpRequest.head(goFastDfsUrl + "/" + group + "/big/upload/" + secretKey);
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            String name = request.getHeader(key);
            log.info("head request header: {} - {}", key, name);
            patch.header(key, name);
        }
        HttpResponse execute = patch.execute();
        for (Map.Entry> entry : execute.headers().entrySet()) {
            if ("Access-Control-Allow-Origin".equals(entry.getKey())) {
                response.setHeader("Access-Control-Allow-Origin", "*");
                continue;
            }
            String json = String.join(",", entry.getValue());
            log.info("head response header: {} - {}", entry.getKey(), json);
            response.setHeader(entry.getKey(), json);
        }
    }

    /**
     * 上传
     * @param request
     * @param response
     * @param secretKey
     */
    @PatchMapping(value = "group1/big/upload/{secretKey}")
    public void tusForward4Patch(HttpServletRequest request, HttpServletResponse response, @PathVariable String secretKey) {
        HttpRequest patch = HttpRequest.patch(goFastDfsUrl + "/" + group + "/big/upload/" + secretKey);
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            String name = request.getHeader(key);
            log.info("patch request header: {} - {}", key, name);
            patch.header(key, name);
        }
        HttpConnection connection = HttpConnection
            .create(patch.getUrl(), null)
            .setConnectTimeout(-1)
            .setReadTimeout(-1)
            .setMethod(patch.getMethod())
            .setInstanceFollowRedirects(true)
            .header(patch.headers(), true);
        HttpURLConnection httpURLConnection = connection.getHttpURLConnection();
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setChunkedStreamingMode(0);
        try (OutputStream out = httpURLConnection.getOutputStream(); ServletOutputStream outputStream = response.getOutputStream()) {
            out.write(IoUtil.readBytes(request.getInputStream()));
            out.flush();

            byte[] buffer = new byte[1024];
            /*ServletInputStream inputStream = request.getInputStream();
            while (true) {
                int read = inputStream.read(buffer);
                if (read <= 0) break;
                out.write(buffer, 0, read);
            }*/

            response.setStatus(httpURLConnection.getResponseCode());
            Map> headerFields = httpURLConnection.getHeaderFields();
            for (String key : headerFields.keySet()) {
                String value = httpURLConnection.getHeaderField(key);
                log.info("patch response header: {} - {}", key, value);
                response.setHeader(key, value);
            }

            InputStream responseInputStream = httpURLConnection.getInputStream();
            while (true) {
                int read = responseInputStream.read(buffer);
                if (read <= 0) break;
                outputStream.write(buffer, 0, read);
            }
            httpURLConnection.disconnect();
        } catch (Exception e) {
            log.error("patch tus forward: ", e);
            httpURLConnection.disconnect();
        } finally {
            httpURLConnection.disconnect();
        }
    }
}

你可能感兴趣的:(js-tus-client + springboot + go-fastdfs文件服务器 转发tus请求到go-fastdfs服务器)