获取PDF路径后,默认下载,怎样修改为打开!

获取PDF路径后,默认下载,怎样修改为打开!

需求分析:
	我的程序接收的是一个默认的下载路径,比如(locallhost:8080/zpt/viewsd/123132312njk123nm123),
也就是说正常的访问此路径,就是直接对此路径的文件进行下载。现在需要将下载变为预览。
思路分析:
将文件转换为字节码,然后通过流的形式书输出到浏览器上即可。
:Controller代码
@RequestMapping("/zfpt/gg/zjcl")
@Controller
public class PFDServlet {

	// 这个注入是自己的一个工具类。主要就是查找数据库,和把数据转换为字节
    @Autowired
    private UploadService uploadService;

    @ResponseBody
    @RequestMapping("/viewPDF")
    public void viewZjclPDF(HttpServletResponse response, HttpServletRequest request) {
        ServletOutputStream outputStream = null;
        try {
            String fid = request.getParameter("fid");
            Map<String, Object> fileDataById = uploadService.getFileDataById(fid);
            // 得到PDF的字节数组
            byte[] fileData = (byte[]) fileDataById.get("fileData");
            outputStream = response.getOutputStream();
            // 输出
            outputStream.write(fileData);
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
jsp页面代码:
 
<c:forEach var="file" items="${filesInfo}">
    <c:choose>
        <c:when test="${type eq 'image'}">
            <div style="margin-bottom:4px;" class="image-div" fid="${file['id']}" ftype="${file['ftype']}"
                 href="${file['url']}"></div>
        </c:when>
        <c:when test="${type eq 'file'}">
            <c:choose>
                <c:when test="${filescount ne '1' or file['type'] ne 'pdf'}">
                    <c:choose>
                        <c:when test="${file['type'] eq 'pdf'}">
                            <div style="margin-bottom:20px;margin-top:10px;">
                                <a href="${ctx}/zfpt/gg/zjcl/viewPDF?fid=${file['id']}" target="_blank" fid='${file['id']}'><img src="${ctx}/static/rdp/images/filetype/${fn:toLowerCase(file['type'])}.gif" />${file['name']}</a>
                            </div>
                        </c:when>
                        <c:otherwise>
                            <div style="margin-bottom:20px;margin-top:10px;">
                                <a href="${file['url']}" fid='${file['id']}'><img src="${ctx}/static/rdp/images/filetype/${fn:toLowerCase(file['type'])}.gif" />${file['name']}</a>
                            </div>
                        </c:otherwise>
                    </c:choose>

                </c:when>
                <c:otherwise>
                    <c:if test="${not empty  file['id']}">
                        <script>
                            $(function () {
                                window.open("${ctx}/zfpt/gg/zjcl/viewPDF?fid=${file['id']}", "_self");
                            })
                        </script>
                    </c:if>
                </c:otherwise>
            </c:choose>
        </c:when>
        <c:otherwise>
            <div style="margin-bottom:20px;margin-top:10px;">
                <a href='${file['url']}' fid='${file['id']}'><img src="${ctx}/static/rdp/images/filetype/${fn:toLowerCase(file['type'])}.gif" />${file['name']}</a>
            </div>
        </c:otherwise>
    </c:choose>
</c:forEach>
在这个jsp页面主要是进行判断是下载还是预览的效果,主要的代码还是Controller层。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值