⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 registeraction.java

📁 Java数据库编程实例sdfasdfasdfasdfasfdaf
💻 JAVA
字号:
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.user.struts.action;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.user.struts.form.RegisterForm;
import com.user.struts.util.User;

/** 
 * MyEclipse Struts
 * Creation date: 08-08-2007
 * 
 * XDoclet definition:
 * @struts.action path="/RegisterAction" name="RegisterForm" input="/register.jsp" scope="request" validate="true"
 * @struts.action-forward name="error" path="/error.jsp"
 * @struts.action-forward name="success" path="/success.jsp"
 */
public class RegisterAction extends Action {
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			 String encoding = request.getCharacterEncoding();
		     if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))){
		    	 response.setContentType("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
		     }
		     RegisterForm rf = (RegisterForm ) form;
		     FormFile file = rf.getPhoto();//取得上传的文件
		     try {
		    	 String inputFileName=file.getFileName();
		    	 int len=inputFileName.length();
		    	 String fileType=new StringBuffer().append(inputFileName.charAt(len-2))
		    	 				.append(inputFileName.charAt(len-1)).append(inputFileName.charAt(len)).toString();
		    	 String userName=rf.getUserName();
		    	 InputStream stream = file.getInputStream();//把文件读入
		         String filePath = request.getRealPath("/");//取当前系统路径
		         ByteArrayOutputStream baos = new ByteArrayOutputStream();
		         String photo=filePath+"/"+userName+fileType;
		         OutputStream bos = new FileOutputStream(photo);//建立一个上传文件的输出流
		         //System.out.println(filePath+"/"+file.getFileName());
		         int bytesRead = 0;
		         byte[] buffer = new byte[8192];
		         while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
		        	 bos.write(buffer, 0, bytesRead);//将文件写入服务器
		         }
		         bos.cly
		         ose();
		         stream.close();
		          
		         ActionErrors errors=new ActionErrors();
		  		String password=rf.getPassword();
		  		String password2=rf.getPassword2();
		  		String addr=rf.getAddr();
		  		String email=rf.getEmail();
//		  		FormFile photo=rf.getPhoto();
		  		User user=new  User(userName,password,addr,email,photo);
		  		HttpSession session=request.getSession();
		  		session.setAttribute("user", user);
		  		return mapping.findForward("success");
		        }catch(Exception e){
		          System.err.print(e);
		          return mapping.findForward("error");
		        }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -