UploadFile.java
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts.upload.FormFile;
- public class UploadFile {
- private static int BYTESIZE_MAX=8192;
- private String FilePath="/image";//图片存储文件默认 /images
- private String ServerPath; //服务器图片存储路径
- private String FileName; //图片文件名
- public String getServerPath() {
- return ServerPath;
- }
- public void setServerPath(String serverPath) {
- ServerPath = serverPath;
- }
- public String getFileName() {
- return FileName;
- }
- public void setFileName(String fileName) {
- FileName = fileName;
- }
- public void upLoad(FormFile file,HttpServletRequest request)
- {
- try{
- InputStream file_is=file.getInputStream();
- ServerPath=request.getSession().getServletContext().getRealPath(FilePath);
- System.out.println(ServerPath);
- String fileName=file.getFileName();
- String filePath=ServerPath+"/"+fileName;
- OutputStream file_os=new FileOutputStream(filePath);
- int bytesRead=0;
- byte[] buffer=new byte[BYTESIZE_MAX];
- while((bytesRead=file_is.read(buffer, 0, BYTESIZE_MAX))!=-1)
- {
- file_os.write(buffer,0,bytesRead);
- }
- file_os.close();
- file_is.close();
- }
- catch(Exception e)
- {
- }
- }
- }
FileUploadActionManager.java
- /*
- * Generated by MyEclipse Struts
- * Template path: templates/java/JavaClass.vtl
- */
- package file.com.qdrk.www.web.action;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.actions.DispatchAction;
- import org.apache.struts.upload.FormFile;
- import org.apache.struts.validator.DynaValidatorForm;
- import file.com.qdrk.www.web.util.UploadFile;
- /**
- * MyEclipse Struts
- * Creation date: 01-05-2009
- *
- * XDoclet definition:
- * @struts.action path="/fileUploadActionManager" name="FileUploadForm" parameter="method" scope="request" validate="true"
- */
- public class FileUploadActionManager extends DispatchAction {
- private UploadFile uf=null;
- public UploadFile getUf() {
- return uf;
- }
- public void setUf(UploadFile uf) {
- this.uf = uf;
- }
- public ActionForward upload(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- DynaValidatorForm fileForm=(DynaValidatorForm)form;
- FormFile file=(FormFile)fileForm.get("file");
- uf.upLoad(file, request);
- return mapping.findForward("upload");
- }
- }
struts-config.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
- <struts-config>
- <form-beans >
- <form-bean name="fileUploadForm" type="org.apache.struts.validator.DynaValidatorForm">
- <form-property name="file" type="org.apache.struts.upload.FormFile"></form-property>
- </form-bean>
- </form-beans>
- <global-exceptions />
- <global-forwards />
- <action-mappings >
- <action
- attribute="fileUploadForm"
- name="fileUploadForm"
- parameter="method"
- path="/fileUploadActionManager"
- scope="request"
- validate="false"
- type="org.springframework.web.struts.DelegatingActionProxy" >
- <forward name="upload" path="/page/file/uploadfile.jsp"></forward>
- </action>
- </action-mappings>
- <message-resources parameter="cn.qdrk.struts.web.ApplicationResources" />
- <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
- <set-property property="pathnames" value="/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validations.xml" />
- </plug-in>
- <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-property property = "contextConfigLocation" value = "/WEB-INF/action-servlet.xml"/>
- </plug-in>
- </struts-config>
applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName"
- value="com.mysql.jdbc.Driver">
- </property>
- <property name="url" value="jdbc:mysql://localhost:3306/qdrk"></property>
- <property name="username" value="root"></property>
- <property name="password" value="123"></property>
- <property name="defaultAutoCommit" value="true" ></property>
- <property name="removeAbandonedTimeout" value="60"></property>
- <property name="logAbandoned" value="true"></property>
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </prop>
- </props>
- </property>
- </bean>
- <bean id="uploadFile" class="file.com.qdrk.www.web.util.UploadFile">
- </bean>
- </beans>
action-servlet.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
- <bean name="/fileUploadActionManager" class="file.com.qdrk.www.web.action.FileUploadActionManager">
- <property name="uf" ref="uploadFile"></property>
- </bean>
- </beans>
在Myeclipse 7.0 M1 下spring2.5 struts1.3 下调试通过
8401



被折叠的 条评论
为什么被折叠?



