`

将图片等文件上传到数据库

    博客分类:
  • J2EE
阅读更多

数据库中使用blob类型存放此类文件

 

界面

<input type="file" name="file" id="theFile"/>

 

action层

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class SystemAction extends ActionUtils implements ModelDriven<RoleUserComman> {
	@Resource
	private SystemService systemService;
                private File file;
public String uploadPhoto() throws Exception {
		systemService.uploadPhoto(this.getFile());
		return SUCCESS;
	}

 

Service层

@Service
public class SystemService {
	@Resource
	private SystemDao systemDao;
	public void uploadPhoto(File file) throws Exception {
		systemDao.uploadPhoto(file);
	}
..........................

 

dao层

public void uploadPhoto(File file) throws Exception {
  Connection con = DB.getConnection(); 
  PreparedStatement  psta=con.prepareStatement("insert into zxtagl_tb_psrole_user_comman(sessions,username,name,photo,isconvener) values(?,?,?,?,?)");
  System.out.println("上传的文件"+file);
  InputStream in=new BufferedInputStream(new FileInputStream(file));
  psta.setInt(1, 1104);
  psta.setString(2, "gzry");
  psta.setString(3, "工作人员");
  psta.setBinaryStream(4, in, (int)file.length());
  psta.setString(5, "N" );
  psta.execute();
  in.close();
  con.close();
 }

 就这样就可以完成了,当然不要忘记配置action

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics