如下代码,是java后台的一个response,为什么下载docx没有问题,而下载xlsx跟png、txt有问题?
`
private String getYxPicById(HttpServletRequest request,HttpServletResponse response) {
try {
request.setCharacterEncoding("utf-8");
String YxPicId = (String) request.getParameter("YxPicId");
String ATTACHMENT_NAME = (String) request.getParameter("ATTACHMENT_NAME");
ATTACHMENT_NAME = URLDecoder.decode(ATTACHMENT_NAME, "UTF-8");
logger.info("ATTACHMENT_NAME:"+ATTACHMENT_NAME);
logger.info("YxPicId:"+YxPicId);
boolean result = getPicFromYxpt(request,YxPicId,ATTACHMENT_NAME);
response.setHeader("Content-Disposition", "attachment;filename="+ATTACHMENT_NAME);
response.setContentType("application/x-msdownload");
if (result) {
ServletContext ctx = request.getSession().getServletContext();
InputStream is = ctx.getResourceAsStream("/upload/attachment/"+ATTACHMENT_NAME);
OutputStream os = response.getOutputStream();
byte[] buffer = new byte[1024];
int i = -1;
while ((i = is.read(buffer)) != -1) {
os.write(buffer, 0, i);
}
os.flush();
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已经指定数据格式为word。