java 输出文件问题 文件已经生成,直接给定url即可下载。前提是生成的文件在web目录中。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class FileDownloadServlet extends BaseHttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void init() throws ServletException { } private static HashMap<String,String> _MIME_TYPE_MAP = new HashMap<String,String>(); static { _MIME_TYPE_MAP.put("jpeg", "image/jpeg"); _MIME_TYPE_MAP.put("jpg", "image/jpeg"); _MIME_TYPE_MAP.put("jpe", "image/jpeg"); _MIME_TYPE_MAP.put("png", "image/png"); } private void setResponseContentType(HttpServletResponse response, String filetype, String description) throws Exception { String lowerFileType = filetype.toLowerCase(); String contentType = (String) _MIME_TYPE_MAP.get(lowerFileType); //不是图片格式信息 response.setHeader("Content-Type", contentType); response.setHeader("Content-Disposition", "attachment; filename=\"" + java.net.URLEncoder.encode(description,"UTF-8") +"\""); } @SuppressWarnings("unchecked") public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { setResponseContentType(response, filetype, "hhhhhhhhh"); OutputStream out = response.getOutputStream(); java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream( out); InputStream in = new FileInputStream(new File(filepath)); java.io.BufferedInputStream bin = new java.io.BufferedInputStream( in); while (bin.available() > 0) { byte[] b = new byte[512]; int nResult = bin.read(b); if (nResult == -1) break; bout.write(b, 0, nResult); } bout.flush(); } catch (Exception e) { log.error("", e); } } } String filetype,chinese wholesalersuggdescription) throws Exception {String lowerFileType = filetype.toLowerCase();String contentType = (String) _MIME_TYPE_MAP.get(lowerFileType); 基本数据类型和引用类型的在内存中的分配 请教SWT 界面编程问题,懂的进 请问为什么会 java.lang.NoClassDefFoundError? 有回答就结贴 在线等 正方形继承长方形违反liskov原则? 线程的问题 总结一下java命令的语法吧! 散分 学习中碰到的一个特别奇怪的问题,送100分! 初用JDK 1.4.1????困惑!! 任何比较两个StringBuffer对象是否相同 异或符的操作,新手提问 请教一个问题
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void init() throws ServletException {
}
private static HashMap<String,String> _MIME_TYPE_MAP = new HashMap<String,String>();
static {
_MIME_TYPE_MAP.put("jpeg", "image/jpeg");
_MIME_TYPE_MAP.put("jpg", "image/jpeg");
_MIME_TYPE_MAP.put("jpe", "image/jpeg");
_MIME_TYPE_MAP.put("png", "image/png");
}
private void setResponseContentType(HttpServletResponse response,
String filetype, String description) throws Exception {
String lowerFileType = filetype.toLowerCase();
String contentType = (String) _MIME_TYPE_MAP.get(lowerFileType);
//不是图片格式信息
response.setHeader("Content-Type", contentType);
response.setHeader("Content-Disposition", "attachment; filename=\""
+ java.net.URLEncoder.encode(description,"UTF-8") +"\"");
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
setResponseContentType(response, filetype, "hhhhhhhhh");
OutputStream out = response.getOutputStream();
java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream(
out);
InputStream in = new FileInputStream(new File(filepath));
java.io.BufferedInputStream bin = new java.io.BufferedInputStream(
in);
while (bin.available() > 0) {
byte[] b = new byte[512];
int nResult = bin.read(b);
if (nResult == -1)
break;
bout.write(b, 0, nResult);
}
bout.flush();
} catch (Exception e) {
log.error("", e);
}
}
}
uggdescription) throws Exception {
String lowerFileType = filetype.toLowerCase();
String contentType = (String) _MIME_TYPE_MAP.get(lowerFileType);