jialh
2 天以前 d28ee263b8a6fb95c9fe2add2ec58b7561d3fee9
service-api/src/main/java/com/java110/api/controller/app/file/UserDownloadFileController.java
@@ -2,9 +2,11 @@
import com.java110.core.client.FileUploadTemplate;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.userDownloadFile.UserDownloadFileDto;
import com.java110.dto.user.UserDownloadFileDto;
import com.java110.intf.job.IUserDownloadFileV1InnerServiceSMO;
import com.java110.utils.util.Assert;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.PayUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
@@ -17,6 +19,9 @@
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
@@ -33,22 +38,40 @@
    @Autowired
    private FileUploadTemplate fileUploadTemplate;
    @RequestMapping(path = "/download/{downloadId}", method = RequestMethod.GET)
    public void download(@PathVariable String downloadId, HttpServletRequest request, HttpServletResponse response) {
    // /app/file/userfile/download/
    @RequestMapping(path = "/download/{downloadId}/{token}", method = RequestMethod.GET)
    public void download(@PathVariable String downloadId,
                         @PathVariable String token,
                         HttpServletRequest request, HttpServletResponse response) {
        logger.debug("用户开始下载文件" + downloadId);
        String userId = request.getHeader("user-id");
        UserDownloadFileDto userDownloadFileDto = new UserDownloadFileDto();
        userDownloadFileDto.setDownloadId(downloadId);
        userDownloadFileDto.setDownloadUserId(userId);
        //userDownloadFileDto.setDownloadUserId(userId);
        List<UserDownloadFileDto> userDownloadFileDtos = userDownloadFileV1InnerServiceSMOImpl.queryUserDownloadFiles(userDownloadFileDto);
        Assert.listOnlyOne(userDownloadFileDtos, "文件不存在");
        String tempUrl = userDownloadFileDtos.get(0).getTempUrl();
        String fileName = userDownloadFileDtos.get(0).getFileTypeName()+tempUrl.substring(tempUrl.lastIndexOf("/"));
        String date = DateUtil.getFormatTimeStringB(DateUtil.getCurrentDate());
        String newToken = PayUtil.md5(userDownloadFileDtos.get(0).getDownloadId() + date);
        if (!newToken.equals(token)) {
            throw new IllegalArgumentException("token 失效请刷新页面重新下载");
        }
        String tempUrl = userDownloadFileDtos.get(0).getTempUrl();
        String originalFileName = userDownloadFileDtos.get(0).getFileTypeName() + tempUrl.substring(tempUrl.lastIndexOf("/"));
        String fileName ;
        try {
            fileName = URLEncoder.encode(originalFileName, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        int lastDotIndex = fileName.lastIndexOf(".");
        if (lastDotIndex != -1) { // 确保存在点
            fileName = fileName.substring(0, lastDotIndex);
        }
        response.setHeader("content-type", "application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setContentType("application/octet-stream");