获取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);
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层。