jsp+servlet+javabean中关于图片上传的问题

1,首先是前台的提交表单页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>




 
		人员增加页面



   文件上传 - 使用jspsmart upload组件

 

图片:  
 

用户:  
 

 

邮箱:  
 

 

     

2,处理jsp页面传过来的数据:

package action;


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


import model.member;






import com.jspsmart.upload.*;


import dao.addmember;
public class addform extends HttpServlet {
 
 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
private ServletConfig config;
 /**
 * 初始化Servlet
 */
 final public void init(ServletConfig config) throws ServletException {
  this.config = config;
 }
 
 /**
 * 处理GET请求
 */
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  PrintWriter out = response.getWriter();
  out.println("");
  out.println("");
  out.println("

jspSmartUpload : Servlet Sample

"); out.println("

"); out.println("The method of the HTML form must be POST."); out.println(""); out.println(""); } /** * 响应POST请求 */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("

jspSmartUpload : Servlet Sample

"); out.println("
"); SmartUpload mySmartUpload = new SmartUpload(); try { // 初始化 mySmartUpload.initialize(config,request,response); // 上载 mySmartUpload.upload(); com.jspsmart.upload.File f1 = mySmartUpload.getFiles().getFile(0); String name = f1.getFileName(); // 保存上载文件到指定目录 //需要在webroot目录下面新建一个upload文件夹 mySmartUpload.save("upload"); String email=mySmartUpload.getRequest().getParameter("email"); //只能用这种方法才能接收到数据,不然用request.getparameter()取得话只能是null String memberName=mySmartUpload.getRequest().getParameter("memberName"); //email,memberName为form表单提交过来的 String imageFile = name;//图片名字 member m = new member(); m.setEmail(email); m.setImageFile(imageFile); m.setMemberName(memberName); //这边是dao层处理的增加,加入数据库的是图片的名字 addmember a = new addmember(); boolean q = false; q = a.add(m); if(q){ request.getRequestDispatcher("success.jsp").forward(request,response); request.getSession().setAttribute("path", m.getImageFile());//把图片的名字返回给前台页面进行显示 }else{ response.sendRedirect("login.html"); } } catch (Exception e){ out.println("Unable to upload the file.
"); out.println("Error : " + e.toString()); } out.println(""); out.println(""); } }
3,这边是数据库的增加,自己改成自己的就可以了。主要代码就这么多

package dao;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import util.helper;

import model.member;

public class addmember {

	public boolean add(member m) {
		
		Connection connection = helper.getConnection();
		PreparedStatement preparedStatement = null;

		String memberName = m.getMemberName();
		String email = m.getEmail();
		String file = m.getImageFile();
		
		
		int sign = 0;
		String sql = "INSERT into member (memberName,email,imageFile) VALUES(?,?,?)";
		try {
			memberName = new String(memberName.getBytes("ISO8859-1"), "UTF-8");
			email = new String(email.getBytes("ISO8859-1"),"UTF-8");
			file = new String(file.getBytes("ISO8859-1"),"UTF-8");
			
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, memberName);
			preparedStatement.setString(2, email);
			preparedStatement.setString(3, file);
			sign = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		if (sign == 1) {
			return true;
		} else {
			return false;
		}
	}
	
	

}



你可能感兴趣的:(javaEE)