1.struts配置文件的设置
---------------------------------struts-config.xml------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources/>
<form-beans>
<form-bean name="CoreElementsForm" type="form.CoreElementsForm"/>
</form-beans>
<global-forwards>
</global-forwards>
<action-mappings>
<action path="/back/resource/index"
input="/jyadminback/resource/edit.jsp"
name="CoreElementsForm"
parameter="method"
type="action.ResourceAction">
<forward name="edit"
redirect="false"
contextRelative="true"
path="/jyadminback/resource/edit.jsp"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/>
</plug-in>
<plug-in className="net.sf.navigator.menu.MenuPlugIn">
<set-property property="menuConfig" value="/WEB-INF/menu-config.xml"/>
</plug-in>
</struts-config>
2.写相关的action和form文件
---------------------------------ResourceAction.java---------------------------------
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
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 form.CoreElementsForm;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ResourceAction extends DispatchAction{
public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
return mapping.findForward("edit");
}
public ActionForward saveFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
CoreElementsForm cef = (CoreElementsForm)form;
FormFile file = cef.getResFile();
if(file != null){
String fileName = file.getFileName();
if(fileName != null && fileName.length() != 0){
fileName = fileName.substring(fileName.lastIndexOf("."));
fileName = fileName.toLowerCase();
fileName = Long.toString(System.currentTimeMillis()) + fileName;//设置要存到服务器上的文件名,用服务器上的时间序列命名,已免文件重复和解决了中文文件名的乱码问题
writeFile(file,"f:/aaa/bbb/",fileName);//分别设置上传的文件,存储文件的目录和文件存储的名称
file.destroy();
}
return mapping.findForward("edit");
}
private boolean writeFile(FormFile file,String path,String name){
try {
//retrieve the file data
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
File dir=new File(path);
if(!dir.isDirectory()){
dir.mkdirs();
}
InputStream stream = file.getInputStream();
//write the file to the file specified
OutputStream bos = new FileOutputStream(path+name);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
//close the stream
stream.close();
return true;
}
catch (FileNotFoundException fnfe) {
System.out.println("FileNotFoundException: " + fnfe.toString());
return false;
}
catch (IOException ioe) {
System.out.println("IOException: " + ioe.toString());
return false;
}
}
}
------------------------------------CoreElementsForm --------------------------------
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class CoreElementsForm extends org.apache.struts.action.ActionForm {
private FormFile resFile;
/**
* @return Returns the resFile.
*/
public FormFile getResFile() {
return resFile;
}
/**
* @param resFile The resFile to set.
*/
public void setResFile(FormFile resFile) {
this.resFile = resFile;
}
}
3.jsp文件中的设置
----------------------------- edit.jsp---------------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="com.common.util.StringOperate,java.util.*"%>
<%@ taglib uri="struts-html.tld" prefix="html" %>
<%@ taglib uri="struts-bean.tld" prefix="bean" %>
<%@ taglib uri="struts-logic.tld" prefix="logic" %>
<HTML><HEAD><TITLE></TITLE>
<STYLE>BODY {
FONT-SIZE: 12pt
}
TABLE {
FONT-SIZE: 12pt
}
A:link {
COLOR: #000084; TEXT-DECORATION: none
}
A:visited {
COLOR: #000084; TEXT-DECORATION: none
}
A:hover {
COLOR: black; TEXT-DECORATION: underline
}
.ourfont {
FONT-SIZE: 12pt
}
</STYLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.3790.2666" name=GENERATOR></HEAD>
<BODY bgColor=#e8e8e8 leftMargin=0 topMargin=0>
<html:form action="/back/resource/index.do" οnsubmit="return checkForm();" enctype="multipart/form-data">
<TABLE width=100% border=0>
<TR bgcolor="#c48259">
<TD height="20" colspan="4"> <font color="#FFFFFF"><strong>资源信息
</strong> </font></TD>
</TR>
<TR>
<TD width=78 height="22"> <DIV align=center>上传新资源</DIV></TD>
<TD colSpan=3>
<html:file property="resFile" />(如果上传新资源,将会替换掉已有的资源)
</TD>
</TR>
<TR>
<TD height=22> </TD>
<TD colSpan=3 height=69>
<INPUT type="image" height="22" width="45" src="image/bao.gif" border="0" name="imageField6">
</TD>
</TR>
</TABLE>
</html:form></BODY></HTML>