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

解决方案 »

  1.   

    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();
    }