阿! 终于写完了action ,看起来并没有什么新的东西可以拿来展览,呵呵 您列代码如下
STRUTS ACTION
// Created by Xslt generator for Eclipse.
// XSL : not found (java.io.FileNotFoundException: (Bad file descriptor))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl
package com.phone.struts.action;
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 java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.sql.*;
import javax.sql.*;
import com.phone.struts.form.FileloadForm;
import org.apache.struts.upload.FormFile;
import com.phone.struts.javabean.excelReadBean;
/**
* FileloadAction.java created by EasyStruts - XsltGen.
* http://easystruts.sf.net
* created on 11-21-2004
*
* XDoclet definition:
* @struts:action path="/fileload" name="fileloadForm" input="/form/fileload.jsp" validate="true"
* @struts:action-forward name="success.jsp" path="success.jsp"
* @struts:action-forward name="fail.jsp" path="fail.jsp"
*/
public class FileloadAction extends Action {
// --------------------------------------------------------- Instance Variables
private String fodler=null;
// --------------------------------------------------------- Methods
/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
//Start from here
Connection conn = null;
FileloadForm fileloadForm = (FileloadForm) form;
String fname=fileloadForm.getFileName();
if(!uploadFile( mapping, form, request,response))
{
System.out.println("upload file failed");
return mapping.findForward("fail");
}
System.out.println("file upload successful");
excelReadBean readBean=new excelReadBean();
DataSource dataSource = (DataSource)servlet.getServletContext().getAttribute(
"org.apache.struts.action.DATA_SOURCE");
conn = dataSource.getConnection();
readBean.read("test",fodler + "/"+fname,conn);
return mapping.findForward("success");
}
/***
* this function is used to upload file
* @param mapping
* @param form
* @param request
* @param response
*/
public boolean uploadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
FileloadForm hff = (FileloadForm) form;
if(fodler == null)
{
ActionForward test = (ActionForward)mapping.findForward("pathFile");
fodler = test.getPath();
}
if (fodler == null){
fodler =this.getServlet().getServletContext().getRealPath("/WEB-INF");
}
if (fodler != null){
fodler = fodler.replace('\\', '/').replace('/', File.separatorChar);
}
FormFile file = hff.getFile();
if (file == null ) {
System.out.println("error : file is null");
return false;
}
// Get the name and file size
String fname = file.getFileName();
String size = Integer.toString(file.getFileSize()) + " bytes";
try{
InputStream streamIn = file.getInputStream();
OutputStream streamOut=null;
streamOut = new FileOutputStream(fodler + "/"+fname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
}catch(IOException e)
{
System.out.println("IoException:"+e);
}
// Populate the form bean with the results for display in the View
hff.setFileName(fname);
hff.setFileSize(size);
// Clean up our toys when done playing
file.destroy();
return true;
}
}
可以看到uploadFile()方法用力完成,将客户端的文件上传到,服务器上,而在我们的,主程序里,我们完成了对他的调用,随后调用readBean.read("test",fodler + "/"+fname,conn);完成将xls文件读入数据库.呵呵,初步的轮廓就如此了,可惜要更实用,你必须靠率很多问题,而在此,我想,做个简单的例子,到不用兴师动众。
STRUTS FORM
// Created by Xslt generator for Eclipse.
// XSL : not found (java.io.FileNotFoundException: (Bad file descriptor))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl
package com.phone.struts.form;
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;
import org.apache.struts.action.ActionError;
/**
* FileloadForm.java created by EasyStruts - XsltGen.
* http://easystruts.sf.net
* created on 11-21-2004
*
* XDoclet definition:
* @struts:form name="fileloadForm"
*/
public class FileloadForm extends ActionForm {
// --------------------------------------------------------- Instance Variables
/** fileName property */
private String fileName;
/** file property */
private FormFile file;
/** fileSize property */
private String fileSize;
private boolean isSave;
// --------------------------------------------------------- Methods
/**
* Method validate
* @param ActionMapping mapping
* @param HttpServletRequest request
* @return ActionErrors
*/
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (file == null ) {
errors.add("file", new ActionError("error.file"));
}
return errors;
}
/**
* Method reset
* @param ActionMapping mapping
* @param HttpServletRequest request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
fileName = "";
file = null;
fileSize = "";
isSave=false;
}
/**
* Returns the fileName.
* @return String
*/
public String getFileName() {
return fileName;
}
/**
* Set the fileName.
* @param fileName The fileName to set
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* Returns the file.
* @return FormFile
*/
public FormFile getFile() {
return file;
}
/**
* Set the file.
* @param file The file to set
*/
public void setFile(FormFile file) {
this.file = file;
}
/**
* Returns the fileSize.
* @return String
*/
public String getFileSize() {
return fileSize;
}
/**
* Set the fileSize.
* @param fileSize The fileSize to set
*/
public void setFileSize(String fileSize) {
this.fileSize = fileSize;
}
/**
* @return
*/
public boolean isSave() {
return isSave;
}
/**
* @param b
*/
public void setSave(boolean b) {
isSave = b;
}
}
感觉的出来,注释竟然比程序还占空间,呵呵 感谢xslt 为我们提供了自动转换的方法, 呵呵虽然我还是懒得完成注释,
好久没有玩xsl了,会不会手生呢?
好了最后就是jsp页面了
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ page contentType="text/html; charset=GB2312" %>
<html:html>
<body bottommargin="15" topmargin="15" leftmargin="15" rightmargin="15" marginheight="15" marginwidth="15" bgcolor="white" >
<link rel="stylesheet" type="text/css" media="all" href="<%=request.getContextPath()%>/styles/style.css" />
<script type="text/javascript" src="<%=request.getContextPath()%>/scripts/selectbox.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/scripts/global.js"></script>
<title>文件上传:</title>
</head>
<body bottommargin="15" topmargin="15" leftmargin="15" rightmargin="15" marginheight="15" marginwidth="15" bgcolor="white">
<h1>文件上传:</h1>
<!--
The most important part is to declare your form's enctype
to be "multipart/form-data", and to have an html:file
element that maps to your ActionForm's FormFile property
-->
<html:form action="//fileload.do" enctype="multipart/form-data" >
<html:file property="file" /><html:errors property="file"/></p>
<html:submit />
</p>
</html:form>
<p>
</body>
</html:html>
呵呵,html标签为我们提供了简单方面的操作,可惜很多美化工作,还是得借助css,要知道,我总是很
愤恨它,因为它丑陋的各式,为什么就没fo支持呢?还得用,javascript,我总庆幸我不会写,script,
这样总是让我免予帮别人写页面,可惜也因此我不得不依赖别人,copy它们的js.
好像剩下的工作就剩下,调试了,如果你在看,对你确实如此,可是,我的考虑它的应用环境,呵呵,也学我根本
就不需要上传,假如,再本级工作! 不管怎么说,STRUTS下的开发还是这么的快,真的,以前也会被杂乱的jsp,和
servlet搞得头昏脑胀,呵呵 我想该睡个好觉去了!
除了这些,我不会的东西太多了,还要向您、向大家多多学习。