日期:2014-05-18  浏览次数:20691 次

struts从数据库中读取文件存在服务器上的一个问题,应用服务器使用tomcat
<%
  String sql="select id,name,content,uploadtime,length from attchment where type='contract' and typeid='"+contractId+"'";
  Connection conn=DBconnection.getBDCon();
  PreparedStatement pstmt=conn.prepareStatement(sql);
  ResultSet rs=pstmt.executeQuery();
  while(rs.next())
  {
  String fileId=rs.getString(1);
  String fileName=rs.getString(2);
  InputStream ins=null;
  try{
  ins=rs.getBinaryStream(3);
  }
  catch(Exception ioe)
  {
  ioe.printStackTrace();
  }
  String uploadTime=rs.getString(4);
  int length=rs.getInt(5)/1000+1;
  String nameId=fileId+fileName.substring(fileName.indexOf("."));
  if(!fileName.equals(""))
  {
  try{
  File ff=null;
  ff=new File("webapps\\MCM\\temp\\",nameId);//该路径为tomcat中的webapps目录,MCM是工程名,temp 是工程下的一个文件夹
  if(ff.exists())
  {
  ff.delete();
  }
  else
  ff.createNewFile();
  FileOutputStream fouts=new FileOutputStream(ff);
  byte[] buffer=new byte[rs.getInt(5)];
  ins.read(buffer);
  fouts.write(buffer);
  fouts.flush();
  ins.close();
  fouts.close();
  }
  catch(IOException ioe)
  {
  ioe.printStackTrace();
  }
  }
  %>
  <tr>
  <td><%=fileId%></td>
  <td><a href="<%=request.getContextPath()%>/temp/<%=nameId%>"><%=fileName%></a></td>//下载连接
  <td><%=uploadTime%></td>


以上是写在视图jsp页面上的代码,但是浏览页面时,说FileNotFountException,其实文件并没有在指定的文件夹下生成,哪位大虾能纠正一下,感激不尽!

------解决方案--------------------
你写个java类从数据库读文件,之后写在本地上看看.
何必启动tomcat,使用jsp呢.