如何在jsp页中实现浏览本地硬盘文件夹的功能 类似type="file"不过不是打开文件夹,而是打开目录。点击保存按钮的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <input type="file" name="mf"> <input type="submit" value="上传"> response.setContentType("application/x-msdownload");response.setHeader("Content-disposition","attachment; filename=测试的.txt"); input[file] 不能选择目录,只能选择文件后 把文件名去掉来取得目录 你是说上传一个文件夹目录中的所有文件吧。这个好像要用flash吧。 希望有“保存文件”那样的对话框。 ------------------- 这不就是弹出下载文件对话框吗 写个servlet调用我这个方法 (PS:非本人所写,该方法由layueer写的)本人拿来借用而已 private void down_FILE(HttpServletRequest req,HttpServletResponse resp,String szPath_file,String szFile_name) { String szServer_path= " "; szServer_path=szPath_file; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //和java.io.PrintWriter out 不能共用 ServletOutputStream outm = null; try { outm=resp.getOutputStream(); } catch(Exception e) { exceptionOutput(resp,e.toString()+ " abcd "); } resp.setContentType( "application/octet-stream " ); // MIME type for exe resp.setHeader( "Content-disposition ", "attachment; filename= "+szFile_name);//显示的随机文件名,可以用这个保存或另起一个名 // Open file File f1; szServer_path = szServer_path+ "filename.xls " ; try { f1=new File(szServer_path); FileInputStream fis=new FileInputStream(f1); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(outm); byte[] buff = new byte[2048]; int bytesRead; while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch(Exception e) { } } finally { try { if (bis != null) bis.close(); if (bos != null) bos.close(); } catch(IOException e) { } } } servlet不能用啊,我用的struts2框架,会被拦截的 import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileUploadException;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;上传、下载你可以下commons包,当然也可以别servlet中代码: //防止路径中有汉字 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); //判断是否multipart模式 boolean flag = ServletFileUpload.isMultipartContent(request); if(flag){ DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(4096); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(1000000); Iterator i = null; try { i = upload.parseRequest(request).iterator(); } catch (FileUploadException e1) { e1.printStackTrace(); } while(i.hasNext()){ FileItem fi = (FileItem)i.next(); String fileName = fi.getName(); try { String name = fileName.substring( fileName.lastIndexOf("\\")+1,fileName.length()); String realpath =request.getRealPath("/uploadFill"); String ContextPath =request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ContextPath+"/"; String remoteAddr = request.getRemoteAddr(); String LocalAddr = request.getLocalAddr(); Date date = new Date(); String filename=date.getYear()+date.getMonth()+date.getDay()+date.getHours()+date.getMinutes()+date.getSeconds()+name; System.out.println(filename); System.out.println(date.getYear()+""+date.getMonth()+date.getDay()+date.getHours()); String filepath=realpath+File.separatorChar+filename; System.out.println("filepath:"+filepath); //// write直接输出 fi.write(new File(realpath, filename)); request.getSession().setAttribute("iamge", "uploadFill/"+filename); System.out.println("uploadFill/"+filename); request.getRequestDispatcher("MyFillUpLoad.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); } } }else{ System.out.println("否multipart模式"); } 其实如果是下载。rar或者是看图片,直接写response.sendRedirect(“你得到的目录”);就可以 其实我只是想获得C:\backup\leadpdb20120917.dmp这样的东西。当然不能手动敲进去 问题出在什么地方 css填充整个页面 javaWeb 求助 可能是一个很简单的问题,但我却不知道怎么办?xdjm请进 如何在JSP中写存储过程并执行?急,在线等! rmic编译的问题,怪!!!!急!!!!! 不好说这是种什么问题 求教一个标签问题,[#include "../include/footer.html"/] web项目打开的时候偶尔显示源码 myeclipse反向工程有什么缺点 公司不让用 谁有好的html5教程
<input type="submit" value="上传">
response.setHeader("Content-disposition","attachment; filename=测试的.txt");
------------------- 这不就是弹出下载文件对话框吗 写个servlet调用我这个方法 (PS:非本人所写,该方法由layueer写的)
本人拿来借用而已 private void down_FILE(HttpServletRequest req,HttpServletResponse resp,String szPath_file,String szFile_name)
{
String szServer_path= " ";
szServer_path=szPath_file;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
//和java.io.PrintWriter out 不能共用
ServletOutputStream outm = null;
try
{
outm=resp.getOutputStream();
}
catch(Exception e)
{
exceptionOutput(resp,e.toString()+ " abcd ");
}
resp.setContentType( "application/octet-stream " ); // MIME type for exe
resp.setHeader( "Content-disposition ", "attachment; filename= "+szFile_name);//显示的随机文件名,可以用这个保存或另起一个名
// Open file
File f1;
szServer_path = szServer_path+ "filename.xls " ;
try
{
f1=new File(szServer_path);
FileInputStream fis=new FileInputStream(f1);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(outm);
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff, 0, bytesRead);
} }
catch(Exception e)
{
}
}
finally
{
try
{
if (bis != null) bis.close();
if (bos != null) bos.close();
}
catch(IOException e)
{
}
}
}
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
上传、下载你可以下commons包,当然也可以别
servlet中代码:
//防止路径中有汉字
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//判断是否multipart模式
boolean flag = ServletFileUpload.isMultipartContent(request);
if(flag){
DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(1000000);
Iterator i = null;
try {
i = upload.parseRequest(request).iterator();
} catch (FileUploadException e1) {
e1.printStackTrace();
}
while(i.hasNext()){
FileItem fi = (FileItem)i.next();
String fileName = fi.getName();
try {
String name = fileName.substring( fileName.lastIndexOf("\\")+1,fileName.length());
String realpath =request.getRealPath("/uploadFill");
String ContextPath =request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ContextPath+"/";
String remoteAddr = request.getRemoteAddr();
String LocalAddr = request.getLocalAddr(); Date date = new Date();
String filename=date.getYear()+date.getMonth()+date.getDay()+date.getHours()+date.getMinutes()+date.getSeconds()+name;
System.out.println(filename);
System.out.println(date.getYear()+""+date.getMonth()+date.getDay()+date.getHours());
String filepath=realpath+File.separatorChar+filename;
System.out.println("filepath:"+filepath);
//// write直接输出
fi.write(new File(realpath, filename));
request.getSession().setAttribute("iamge", "uploadFill/"+filename);
System.out.println("uploadFill/"+filename);
request.getRequestDispatcher("MyFillUpLoad.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}else{
System.out.println("否multipart模式");
}