读书人

struts中下传文件的代码(没有限制下传

发布时间: 2012-11-03 10:57:44 作者: rapoo

struts中上传文件的代码(没有限制上传文件类型)

?

UpLoadImgForm.java代码:

/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */package com.jqqd.struts.formAction;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionMapping;import org.apache.struts.upload.FormFile;/**  * MyEclipse Struts * Creation date: 06-06-2008 *  * XDoclet definition: * @struts.form name="upLoadImg" */public class UpLoadImgForm extends ActionForm {/* * Generated Methods */private FormFile file;/** *  */private static final long serialVersionUID = 1L;/**  * Method validate * @param mapping * @param request * @return ActionErrors */public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {// TODO Auto-generated method stubreturn null;}/**  * Method reset * @param mapping * @param request */public void reset(ActionMapping mapping, HttpServletRequest request) {// TODO Auto-generated method stub}public FormFile getFile() {return file;}public void setFile(FormFile file) {this.file = file;}}

?UpLoadImgAction.java代码

/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */package com.jqqd.struts.action;import java.io.ByteArrayOutputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;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.jqqd.struts.formAction.UpLoadImgForm;/** * MyEclipse Struts Creation date: 06-06-2008 *  * XDoclet definition: *  * @struts.action path="/upLoadImg" name="upLoadImg" input="/uploadImg.jsp" *                scope="request" validate="true" * @struts.action-forward name="error" path="/error.jsp" * @struts.action-forward name="success" path="/success.jsp" */public class UpLoadImgAction 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) {if (form instanceof UpLoadImgForm) {// 如果form是uploadsFormString encoding = request.getCharacterEncoding();if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {response.setContentType("text/html; charset=gb2312");// 如果没有指定编码,编码格式为gb2312}UpLoadImgForm upLoad = (UpLoadImgForm) form;FormFile formFile = upLoad.getFile();try {InputStream stream = formFile.getInputStream();String realPath = request.getRealPath("/"+"upload");ByteArrayOutputStream baos = new ByteArrayOutputStream();OutputStream bao = new FileOutputStream(realPath + "/"+ formFile.getFileName());int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {bao.write(buffer, 0, bytesRead);}bao.flush();bao.close();stream.close();} catch (Exception e) {System.out.println(e);}return mapping.findForward("success");}return mapping.findForward("error");}}

?

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="upLoadImg" type="com.jqqd.struts.formAction.UpLoadImgForm" />         </form-beans>  <global-exceptions />  <global-forwards />  <action-mappings >        <action      attribute="upLoadImg"      validate="false"      name="upLoadImg"      path="/upLoadImg"      scope="request"      type="com.jqqd.struts.action.UpLoadImgAction">      <forward name="error" path="/error.jsp" />      <forward name="success" path="/success.jsp" />    </action>          </action-mappings>  <message-resources parameter="com.jqqd.struts.ApplicationResources" /></struts-config>

?uploadImg.jsp文件的代码:

<%@ page language="java" pageEncoding="GB2312"%><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %><html>  <head>    <title>uploadImg.do</title>    <link type="text/css" rel="stylesheet" href="css/upload.css" />  </head>  <body>  <html:form action="upLoadImg.do" enctype="multipart/form-data">  <div id="uploadD">  <div id="uploadTitD">图片上传</div>  <div id="uploadConD">  <html:file property="file"></html:file><br/><br/>  <html:submit></html:submit><html:reset></html:reset></div>  </div> </html:form>    </body>  </html>

?

?base.css代码:

html,body{margin:0px;border:0px; background-color:#333333; text-align:center; font-size:12px; color:#FFFFFF; }body img,body div{border:0px; margin-left:auto; margin-right:auto;}

?upload.css代码:

?

@import url(base.css);#uploadD{width:600px; height:500px; border:1px solid #FFFFFF; margin-top:50px;}#uploadTitD,#uploadConD{width:600px; height:30px; border:0px; background-color:#999999; line-height:2.5em; height:2.5em;}#uploadConD{background-color:#666666;}

?

读书人网 >软件架构设计

热点推荐