|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
C#是不行的,比如说美国的航天飞船里就有java开发的程序以上是我的愚见,其实不管那种语言,你学好了,都能找到好的工作,上传比来在做Struts项目时碰到了上传多个文件的成绩。在网上查了很多材料,也没有找到用Struts上传多个文件的例子。我经由几天的研讨,完成了用Struts上传多个文件的功效。如今贴出来让人人共享!
一。创建ActionForm
packagecom.cnehu.struts.form;
importjavax.servlet.http.HttpServletRequest;
importorg.apache.struts.action.ActionError;
importorg.apache.struts.action.ActionErrors;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.upload.FormFile;
importorg.apache.struts.upload.MultipartRequestHandler;
/**
*<p>
*Title:UpLoadForm
*</p>
*<p>
*Copyright:Copyright(c)2005techyanghttp://blog.csdn.net/techyang
*</p>
*@authortechyang
*@version1.0
*/
publicclassUpLoadFormextendsActionForm
{
publicstaticfinalStringERROR_PROPERTY_MAX_LENGTH_EXCEEDED="org.apache.struts.webapp.upload.MaxLengthExceeded";
protectedFormFiletheFile;
protectedFormFiletheFile2;
publicFormFilegetTheFile()
{
returntheFile;
}
publicvoidsetTheFile(FormFiletheFile)
{
this.theFile=theFile;
}
publicActionErrorsvalidate(ActionMappingmapping,
HttpServletRequestrequest)
{
ActionErrorserrors=null;
//hasthemaximumlengthbeenexceeded?
BooleanmaxLengthExceeded=(Boolean)request
.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
if((maxLengthExceeded!=null)&&(maxLengthExceeded.booleanValue()))
{
errors=newActionErrors();
errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED,newActionError(
"maxLengthExceeded"));
}
returnerrors;
}
/**
*@returnReturnsthetheFile2.
*/
publicFormFilegetTheFile2()
{
returntheFile2;
}
/**
*@paramtheFile2ThetheFile2toset.
*/
publicvoidsetTheFile2(FormFiletheFile2)
{
this.theFile2=theFile2;
}
}
二。创建ActionServlet
packagecom.cnehu.struts.action;
importjava.io.*;
importjavax.servlet.http.*;
importorg.apache.struts.action.*;
importorg.apache.struts.upload.FormFile;
importcom.cnehu.struts.form.UpLoadForm;
/**
*<p>
*Title:UpLoadAction
*</p>
*<p>
*Copyright:Copyright(c)2005techyanghttp://blog.csdn.net/techyang
*</p>
*@authortechyang
*@version1.0
*/
publicclassUpLoadActionextendsAction
{
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
throwsException
{
Stringencoding=request.getCharacterEncoding();
if((encoding!=null)&&(encoding.equalsIgnoreCase("utf-8")))
{
response.setContentType("text/html;charset=gb2312");//假如没有指定编码,编码格局为gb2312
}
UpLoadFormtheForm=(UpLoadForm)form;
FormFilefile=theForm.getTheFile();//获得上传的文件
FormFilefile2=theForm.getTheFile2();
try
{
/*
*取以后体系路径D:Tomcat5webappscoka个中coka为以后context
*/
StringfilePath=this.getServlet().getServletContext()
.getRealPath("/");
InputStreamstream=file.getInputStream();//把文件读进
ByteArrayOutputStreambaos=newByteArrayOutputStream();
/*
*创建一个上传文件的输入流假如是linux体系请把UploadFiles后的""换成"/"
*/
OutputStreambos=newFileOutputStream(filePath+
"UploadFiles"+file.getFileName());
//D:Tomcat5webappscokaUploadFilesDSC01508.JPG
/*System.out.println(filePath+
"UploadFiles"+file.getFileName());
System.out.println(filePath);*/
request.setAttribute("fileName",filePath+"/"
+file.getFileName());
intbytesRead=0;
byte[]buffer=newbyte[8192];
while((bytesRead=stream.read(buffer,0,8192))!=-1)
{
bos.write(buffer,0,bytesRead);//将文件写进服务器
}
bos.close();
stream.close();
InputStreamstream2=file2.getInputStream();//把文件读进
ByteArrayOutputStreambaos2=newByteArrayOutputStream();
OutputStreambos2=newFileOutputStream(filePath+
"UploadFiles"+file2.getFileName());//创建一个上传文件的输入流
intbytesRead2=0;
byte[]buffer2=newbyte[8192];
inti=0;
while((bytesRead2=stream2.read(buffer2,0,8192))!=-1)
{
bos2.write(buffer2,0,bytesRead2);//将文件写进服务器
}
bos2.close();
stream2.close();
}catch(Exceptione)
{
System.err.print(e);
}
returnmapping.findForward("display");
}
}
三。创建上传用的JSP文件upload.jsp
<%@tagliburi="http://jakarta.apache.org/struts/tags-html"prefix="html"%>
<html:html>
<head>
<title>用Struts上传文件</title>
</head>
<body>
<html:formaction="/uploadsAction"enctype="multipart/form-data">
<html:fileproperty="theFile"/>
<html:fileproperty="theFile2"/>
<html:submit/>
</html:form>
</body>
</html:html>
四。设置struts-config.xml文件
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN""http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources/>
<form-beans>
<form-beanname="uploadsForm"type="com.cnehu.struts.form.UpLoadForm"/>
</form-beans>
<global-exceptions/>
<global-forwards>
</global-forwards>
<action-mappings>
<actionname="uploadsForm"type="com.cnehu.struts.action.UpLoadAction"path="/uploadsAction">
<forwardname="display"path="/display.jsp"/>
</action>
</action-mappings>
</struts-config>
全文完!
Java伴随着互联网的迅猛发展而发展,逐渐成为重要的网络编程语言。Oracle收购Sun后Java前途未卜。 |
|