图片提取到本地文件夹 需求描述:在程序(B/S)中实现从oracle数据库中图片读取到本地的文件夹中,按照数据库中某一个字段分类,文件夹使用此字段值来命名……应该是要用到流,但是我对流的操作不是很熟悉,现在也没多少时间再去深究。有哪位高手能够提供一段示例代码?(越详细越好)谢谢!!!可以再加分!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 blog,inputStream,outputStream String basePath = "c:/pic/";//查询数据。。if (rs.next()) { InputStream is = null; OutputStream os = null; Blob blob = rs.getBlob("picture"); String folderName=rs.getStirng("fname"); byte[] b = new byte[1024]; try{ is = blob.getBinaryStream(); String filePath = basePath+fname; os = new FileOutputStream(filePath); int i = 0; while ((i = is.read(b)) != -1) { os.write(b, 0, i); } os.flush(); }catch(Exception e){} finally{ os.close(); is.close(); }}这样下载的文件是在服务器上 如果是下载到客户端需要用response将数据返回给客户端,然后会出现下载提示框。貌似下载到客户端的话,没有必要按照数据库的字段去建文件夹吧,只能是用户指定保存位置 看样子还是要生成在客户端了,我觉得可以先在服务器生成压缩文件,然后通过response传递给用户 public ActionForward uploadFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { int count=0; JspFactory j=JspFactory.getDefaultFactory(); PageContext pageContext=j.getPageContext(super.getServlet(), request, response,null, true, 8192,true); SmartUpload mySmartUpload=new SmartUpload(); Request req=mySmartUpload.getRequest(); try { mySmartUpload.initialize(pageContext); } catch (ServletException e1) { e1.printStackTrace(); } String title=null; String content=null; String filename=null; File file=new File(); try{ mySmartUpload.upload(); count=mySmartUpload.save(request.getSession(). getServletContext(). getRealPath("/upload/")); filename=mySmartUpload.getFiles().getFile(0).getFileName(); title=req.getParameter("file_title"); content=req.getParameter("file_concent"); file.setFileTitle(title); file.setFileContent(content); file.setFileName(filename); file.setFileId(DateTime.getDateId()); Author author=(Author)request.getSession().getAttribute("currentAuthor"); file.setAuthor(author); Date d=new Date(); SimpleDateFormat dk=new SimpleDateFormat("yyyy-MM-dd"); String date=dk.format(d); file.setFileDate(date); String sign=fileService.upFile(file); if(sign.equals("上传成功")){ request.setAttribute("message", "文件上传成功!"); }else{ request.setAttribute("message", "文件上传失败!"); } }catch(Exception e){ request.setAttribute("message", "文件上传失败!"); e.printStackTrace(); } finally{ ActionForward af=new ActionForward("/Message.jsp"); af.setRedirect(false); return af; } } /** * 功能:下载文件 * 公司: * 制作人:欧阳 * 时间: * */ public ActionForward downloadFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { JspFactory j=JspFactory.getDefaultFactory(); PageContext pageContext=j.getPageContext(super.getServlet(), request, response,null, true, 8192,true); // 得到文件名字 String filename=null; try { filename = new String(request.getParameter("filename").getBytes("ISO-8859-1"),"GBK"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } SmartUpload myDown=new SmartUpload(); try { myDown.initialize(pageContext); } catch (ServletException e1) { e1.printStackTrace(); } try{ myDown.downloadFile(request. getSession().getServletContext(). getRealPath("upload")+"/"+filename); }catch(Exception e){ e.printStackTrace(); } return null; } 首先在服务器的一个目录下生成n个不同的文件到分类文件夹里,这个上面已经说过了然后将这个文件夹生产一个zip文件,看一下jdk文档,不想看的话网上查一下有现成的最后用response返回这个zip文件,这个过程和下载文件差不多 jacob操作outlook 关于错误500的问题二,在线等 现在流行的各种标签及标签组合有哪些? 哪位高手能指点下java读取非本地服务器xml文件信息 Spring的simpleUrlHandlerMapping疑问 DWR关于MarshallException的问题 谁能提供J2EE.jar包下载,急~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ jboss部署错误 SecurityManager怎么实现防止暴力反射 求助!!Oralce日期字段获取出错 ToroisetSVN java中web server的一点问题
inputStream,
outputStream
//查询数据。。
if (rs.next()) {
InputStream is = null;
OutputStream os = null;
Blob blob = rs.getBlob("picture");
String folderName=rs.getStirng("fname");
byte[] b = new byte[1024];
try{
is = blob.getBinaryStream();
String filePath = basePath+fname;
os = new FileOutputStream(filePath);
int i = 0;
while ((i = is.read(b)) != -1) {
os.write(b, 0, i);
}
os.flush();
}catch(Exception e){}
finally{
os.close();
is.close();
}
}
这样下载的文件是在服务器上
HttpServletRequest request, HttpServletResponse response) {
int count=0;
JspFactory j=JspFactory.getDefaultFactory();
PageContext pageContext=j.getPageContext(super.getServlet(), request, response,null, true, 8192,true);
SmartUpload mySmartUpload=new SmartUpload();
Request req=mySmartUpload.getRequest();
try {
mySmartUpload.initialize(pageContext);
} catch (ServletException e1) {
e1.printStackTrace();
}
String title=null;
String content=null;
String filename=null;
File file=new File();
try{
mySmartUpload.upload();
count=mySmartUpload.save(request.getSession().
getServletContext().
getRealPath("/upload/"));
filename=mySmartUpload.getFiles().getFile(0).getFileName();
title=req.getParameter("file_title");
content=req.getParameter("file_concent");
file.setFileTitle(title);
file.setFileContent(content);
file.setFileName(filename);
file.setFileId(DateTime.getDateId());
Author author=(Author)request.getSession().getAttribute("currentAuthor");
file.setAuthor(author);
Date d=new Date();
SimpleDateFormat dk=new SimpleDateFormat("yyyy-MM-dd");
String date=dk.format(d);
file.setFileDate(date);
String sign=fileService.upFile(file);
if(sign.equals("上传成功")){
request.setAttribute("message", "文件上传成功!");
}else{
request.setAttribute("message", "文件上传失败!");
}
}catch(Exception e){
request.setAttribute("message", "文件上传失败!");
e.printStackTrace();
}
finally{
ActionForward af=new ActionForward("/Message.jsp");
af.setRedirect(false);
return af;
}
}
/**
* 功能:下载文件
* 公司:
* 制作人:欧阳
* 时间:
* */
public ActionForward downloadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
JspFactory j=JspFactory.getDefaultFactory();
PageContext pageContext=j.getPageContext(super.getServlet(), request, response,null, true, 8192,true);
// 得到文件名字
String filename=null;
try {
filename = new String(request.getParameter("filename").getBytes("ISO-8859-1"),"GBK");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
SmartUpload myDown=new SmartUpload();
try {
myDown.initialize(pageContext);
} catch (ServletException e1) {
e1.printStackTrace();
}
try{
myDown.downloadFile(request.
getSession().getServletContext().
getRealPath("upload")+"/"+filename);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
然后将这个文件夹生产一个zip文件,看一下jdk文档,不想看的话网上查一下有现成的
最后用response返回这个zip文件,这个过程和下载文件差不多