struts1.2上传文件
struts1.2上传文件
几天代码如下:
DB类代码如下:
package com.company.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DB {
		private static final String driverName="com.mysql.jdbc.Driver";
		private static final String url="jdbc:mysql://localhost:3306/company";
		private static final String user="root";
		private static final String password="123456";
		private Connection conn = null;
		private ResultSet rs=null;
		private PreparedStatement pre=null;
		public void getCon(String sql){
			try {
				Class.forName(driverName);
			} catch (ClassNotFoundException e) {
				System.out.println("找不到驱动");
				e.printStackTrace();
			}
			try {
				conn = DriverManager.getConnection(url, user, password);
			} catch (SQLException e) {
				System.out.println("数据库用户名或密码不对");
				e.printStackTrace();
			}
			try {
				pre=conn.prepareStatement(sql);
			} catch (SQLException e) {
				System.out.println("sql语句创建失败");
				e.printStackTrace();
			}
		}
		public void close(){
			try {
				conn.close();
			} catch (SQLException e) {
				System.out.println("数据库关闭失败");
				e.printStackTrace();
			}
		}
		
		public ResultSet find(String sql,Object o[]){
			getCon(sql);
			try {
				setParmater(o);
				rs = pre.executeQuery();
			} catch (SQLException e) {
				e.printStackTrace();
			}	
			return rs;
		}
		private void setParmater(Object[] o) throws SQLException {
			if(o!=null && o.length>0){
					for(int i=0;i<o.length;i++){
							pre.setObject(i+1, o[i]);
					}
			}
		}
		/**
		 * 添加
		 * @param sql
		 * @param o
		 */
		public boolean insert(String sql,Object o[]){
			getCon(sql); //创建连接
			boolean b = false;
				try {
					setParmater(o);
					b=pre.execute();
				} catch (SQLException e) {	
					System.out.println("执行添加失败");
					e.printStackTrace();
				}finally{
					close();
				}	
			return b;
		}
		/**
		 * 更新
		 * @param sql
		 * @param o
		 */
		public boolean update(String sql,Object o[]){
			getCon(sql); //创建连接
			boolean b = false;
			try {
				setParmater(o);
				b = pre.execute();
			} catch (SQLException e) {
				System.out.println("执行添加失败");
				e.printStackTrace();
			}finally{
				close();
			}	
			return b;
		}
		
	}
postProduct.jsp代码如下:
<form action="/product.do?method=post" method="post" id="form" name="form" enctype="multipart/form-data"> <input type="text" name="title" size="80" class="text-input medium-input" id="title"/> <br /> <br /> 请选择产品图片:<input type="file" name="file1" id="file1" /> <textarea class="text-input textarea wysiwyg" id="content" name="content" cols="79" rows="25" wrap="physical"> </textarea> <br /> <input type="submit" class="button" value="提交" /> </form>
ProductForm.java代码如下:
public class ProductForm extends ActionForm {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3341270232193156840L;
	private Integer id;
	private String title;
	private String content;
	private String image;
	private FormFile file1;
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		return null;
	}
	public void reset(ActionMapping mapping, HttpServletRequest request) {
	}
	public FormFile getFile1() {
		return file1;
	}
	public void setFile1(FormFile file1) {
		this.file1 = file1;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
}
ProductDaoImp.java类  方法类
public void insertSave(ProductForm productForm) {
		String sql = "";
		if (productForm.getId() != null) {
			sql = "update product set title=?,content=?,image=? where id=?";
			this.update(
					sql,
					new Object[] { productForm.getTitle(),
							productForm.getContent(), productForm.getImage(),
							productForm.getId() });
		} else {
			sql = "insert into product(title,content,image) values(?,?,?)";
			this.insert(
					sql,
					new Object[] { productForm.getTitle(),productForm.getContent(),productForm.getImage() });
		}
	}
	public ProductForm getProduct(Integer id) {
		ProductForm productForm = new ProductForm();
		String sql = "select * from product where id=?";
		ResultSet rs = this.find(sql, new Object[] { id });
		try {
			while (rs.next()) {
				productForm.setId(rs.getInt("id"));
				productForm.setTitle(rs.getString("title"));
				productForm.setContent(rs.getString("content"));
				productForm.setImage(rs.getString("image"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return productForm;
	}
struts-config.xml文件代码如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans> <form-bean name="productForm" type="com.company.form.ProductForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <action attribute="productForm" name="productForm" path="/product" scope="request" parameter="method" type="com.company.action.ProductAction"> <forward name="gopost" path="/admin/postProduct.jsp" /> <forward name="post" path="/product.do?method=productlist3" /> </action> </action-mappings> <message-resources parameter="com.company.struts.ApplicationResources" /> </struts-config>
然后就成功了
版权声明:本文为博主原创文章,未经博主允许不得转载。
                    
                
                
            
        
浙公网安备 33010602011771号