Java实现文件另存为 把本地目录下的文件另存 求各位大神

lsh6688 2012-04-12 04:43:43
我在JSP页面上有个连接 具体就是将上传到服务器文件夹下的文件下载到其他地方
点击链接的时候有另存为的对话框 然后选择自己的路径 把该文加件下的文件都另存为其他地方

谢谢!!!!
...全文
913 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kavin2001 2012-04-12
  • 打赏
  • 举报
回复
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();
}
lsh6688 2012-04-12
  • 打赏
  • 举报
回复

如图

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧