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

本文探讨了使用jsp、servlet和javabean进行图片上传时遇到的问题及解决方案,重点在于前端表单提交和后台处理的流程。
摘要由CSDN通过智能技术生成

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

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>人员增加页面</title>
</head>
<body>
<font size="5" color="#FF0000">
   <b>文件上传 - 使用jspsmart upload组件</b>
</font><br>
<form name="addform" enctype="multipart/form-data" method="post" action="addform"  οnsubmit="return validate(this);">
 <p>图片: 
 <input type="file" name="ulfile" size="20" maxlength="80"><br>
 </p>
  <p>用户:
 <input type="text" name="memberName" size="30" maxlength="50"><br>
 </p>
 <p>邮箱:
 <input type="text" name="email" size="30" maxlength="50"><br>
 </p>
 <p> 
 <input type="submit" value="上传">
 <input type="reset" value="清除">
 </p>
</form>

<script type="text/javascript">
	/* 常用的正则表达式
					Require : /.+/,
					Id:/^[0-9]+$/;
					Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
					Phone : /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/,
					Mobile : /^((\(\d{3}\))|(\d{3}\-))?13\d{9}|15[0123456789]\d{8}$/,
					Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
					IdCard : "this.IsIdCard(value)",
					Currency : /^\d+(\.\d+)?$/,
					Number : /^\d+$/,
					Zip : /^[1-9]\d{5}$/,
					QQ : /^[1-9]\d{4,8}$/,
					Integer : /^[-\+]?\d+$/,
					Double : /^\d+(\.\d+)?$/,
				    MyDouble : /^[-\+]?\d+(\.\d+)?$/,
				    English : /^[A-Za-z0-9]+$/,
					Chinese :  /^[\u0391-\uFFE5]+$/,
					Username : /^[a-z]\w{3,}$/i,
					UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/, */
				
				
					function validate(addform)
					{
						if(addform.memberName.value=="")
						{
							alert("请输入正确的名字");
							return false;
						}
						if(!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(addform.email.value)){
								alert("请输入正确的邮箱地址");
								return false;
						}
						return true;//如果两个都判断通过就可以
					} 
					
					
</script>
</body>
</html>
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("<HTML>");
  out.println("<BODY BGCOLOR='white'>");
  out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
  out.println("<HR><BR>");
  out.println("The method of the HTML form must be POST.");
  out.println("</BODY>");
  out.println("</HTML>");
 }
 
 /**
 * 响应POST请求
 */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


  
  PrintWriter out = response.getWriter();
  out.println("<HTML>");
  out.println("<BODY BGCOLOR='white'>");
  out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
  out.println("<HR>");
  
  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.<br>");
   out.println("Error : " + e.toString());
  }
  out.println("</BODY>");
  out.println("</HTML>");
    }
}
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;
		}
	}
	
	

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值