文件上传(仅供个人参考)

(1)DAO
package com.zzu.struts.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zzu.struts.dbc.DBConnection;
import com.zzu.struts.model.Info;
public class InfoDao {
 Connection con=null;
 PreparedStatement ps = null;
 DBConnection dbc = new DBConnection();
 public InfoDao(){
  con = dbc.getCon();
 }
 public boolean addImages(Info info){
  boolean f = false;
  String sql ="insert into info values(?)";
  try {
   ps = con.prepareStatement(sql);
   ps.setString(1, info.getImages());
   int i = ps.executeUpdate();
   if(i>0){
    f = true;
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return f;
 }
 public List showImages(){
  List list = new ArrayList();
  ResultSet rs = null;
  Info info = null;
  String sql = "select * from info";
  try {
   ps = con.prepareStatement(sql);
   rs = ps.executeQuery();
   while(rs.next()){
    info = new Info();
    info.setImages(rs.getString("images"));
    list.add(info);
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return list;
 }
 public static void main(String[] args) {
 /* Info info = new Info();
 // i.setImages("a");
 // System.out.println(new InfoDao().addImages(i));
  List list = new InfoDao().showImages();
  System.out.println(list.size());
  for(int i = 0;i<list.size();i++){
   info = (Info)list.get(i);
   System.out.println(info.getImages());
  }*/
 }
}
Action处理:
public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
   UpForm upForm = (UpForm) form;
   FormFile formFile = upForm.getWenjian();
   PrintWriter out = null;
  response.setCharacterEncoding("GBK");
  out = response.getWriter();
  String path = this.getServlet().getServletContext().getRealPath("/uploads");
  InputStream in = formFile.getInputStream();
  String filePath =path + "//"+ formFile.getFileName();
  FileOutputStream fout = new FileOutputStream(filePath);
  byte buffer[] = new byte[8192];
  int count = 0;
  InfoDao id = new InfoDao();
  Info info = new Info();
   info.setImages(filePath);
  boolean flag = id.addImages(info);  
  
   while ((count = in.read(buffer)) > 0) {
    fout.write(buffer, 0, count);
    if(flag ==true){
      out.println("文件上传..........");
    }
   }
   System.out.println("文件大小" + count);
   fout.close();
   return null;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值