Java实现文件另存为 把本地目录下的文件另存 求各位大神 我在JSP页面上有个连接 具体就是将上传到服务器文件夹下的文件下载到其他地方点击链接的时候有另存为的对话框 然后选择自己的路径 把该文加件下的文件都另存为其他地方谢谢!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 File obj = null; // 实际在服务器上存储的文件名 String realfileName = request.getParameter("realname"); // 用于显示的文件名 String fileName = request.getParameter("filename"); if (request.getParameter("filename") != null) { fileName = new String(fileName.getBytes("ISO-8859-1"), "utf-8"); } String path = AutoSession.file_path + File.separator + realfileName; obj = new File(path); if (!obj.exists()) { response.setContentType("text/html;charset=utf-8"); response.getWriter().print("没有找到文件!请重新上传!"); return; } ServletOutputStream out = response.getOutputStream(); response.setContentType("application/OCTET-STREAM;charset=utf-8"); fileName = fileName.trim(); response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO-8859-1") + ""); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(new FileInputStream(obj)); bos = new BufferedOutputStream(out); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (IOException e) { throw e; } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); } 我想知道在spring aop的环绕通知得到HttpServletReponse对象 Hibernate类型转换 求救呀!!!!!! 关于正则表达式。我想提取字符中的参数,怎么做? linux 中的 session 怎样结束 简单的权限控制 STRUTS2求助 两个table一个from提交的时候得不到输入的值 配置连接池‘成功’后出现的问题!高手请进!! web启动的时候出现如下错误 jstl x:parse 读取xml为空 我的Eclipse安装后没有新建一个Web项目的选项
// 实际在服务器上存储的文件名
String realfileName = request.getParameter("realname");
// 用于显示的文件名
String fileName = request.getParameter("filename");
if (request.getParameter("filename") != null) {
fileName = new String(fileName.getBytes("ISO-8859-1"), "utf-8");
}
String path = AutoSession.file_path + File.separator + realfileName;
obj = new File(path); if (!obj.exists()) {
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("没有找到文件!请重新上传!");
return;
}
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/OCTET-STREAM;charset=utf-8");
fileName = fileName.trim();
response.setHeader("Content-disposition", "attachment;filename="
+ new String(fileName.getBytes("gb2312"), "ISO-8859-1")
+ "");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try { bis = new BufferedInputStream(new FileInputStream(obj));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}