|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
还有就是总有人问我到底该学习什么语言,什么语言有前途,那么我的回答是不论是C,C++,java,.net,ruby,asp或是其他语言都可以学,编程的关键不是语言,而是思想。js|上传在用Java开辟企业器体系的利用,出格是触及到与办公相干的软件开辟的时分,文件的上传是客户常常要提到的请求.因而有一套很好文件上传的办理举措也能便利人人在这一块的开辟.
起首声名,该文章是为了本人纪录一备今后开辟必要的时分,不必从容不迫哈哈........
如今在国际用的十分多的通常为两种办法办理来办理文件上传.
cos.jar+uploadbean.jar+filemover.jar
这个是用的十分广泛的,缘故原由是由于他操纵便利,是我们不用再往存眷,那些文件的输出和输入流,使我们从底层的流中摆脱出来.
UploadFile,UploadBean,MultipartFormDataRequest
<%@pagecontentType="text/html;charset=gb2312"%>
<head>
<title>fbysssUploadBean示例</title>
<!--metahttp-equiv="Content-Type"content="text/html;charset=iso-8859-1"-->
<!--metahttp-equiv="Content-Type"content="text/html;charset=gb2312"-->
</head>
<FORMname="form1"METHOD="POST"ACTION="sssupload.jsp"ENCTYPE="multipart/form-data">
<inputname="title"type="text"value="中笔墨">
<tdclass="bodystyle">附件</td>
<tdclass="bodystyle"><inputname="attach"type="FILE"id="attach"size="50"></td>
<inputname="ok"type="submit"value="提交">
</form>
2.读取表单页面sssgetdata.jsp
<!--
//==========================================================================
//文件:UploadBean上传实例
//功效:办理中文乱码,完成文件上传,并供应上传更名办理计划
//fbysss
//msn:jameslastchina@hotmail.com
//==========================================================================
-->
<%@pagecontentType="text/html;charset=GBK"%>
<%@pagelanguage="java"import="com.jspsmart.upload.*"%>
<%@pageimport="java.text.SimpleDateFormat"%>
<%@pageimport="java.io.File"%>
<%@pageimport="java.util.*"%>
<%@pageimport="javazoom.upload.*"%>
<%@pageimport="uploadutilities.FileMover"%>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
</head>
<%
request.setCharacterEncoding("GBK");//设置编码格局,就不必一个个转码了。
FileMoverfileMover=newFileMover();//你也能够利用自带的实例中jsp:useBean的情势。
UploadBeanupBean=newUploadBean();
MultipartFormDataRequestmrequest=null;
Hashtablefiles=null;
if(MultipartFormDataRequest.isMultipartFormData(request))
{
mrequest=newMultipartFormDataRequest(request,null,100*1024*1024,MultipartFormDataRequest.COSPARSER,"GBK");//注重这里也要设置编码参数
StringsTt0=mrequest.getParameter("title");
out.println("<br>Title0是:"+sTt0+"<br>");
StringsTt1=newString(sTt0.getBytes("ISO-8859-1"),"GBK");
out.println("<br>Title1是:"+sTt1+"<br>");
//这里用来测试title参数是不是准确。调试的时分,加一句if(true)return;便可。
files=mrequest.getFiles();
}
//猎取修正前的文件名
StringsOldFileName=mrequest.getParameter("oldfilename");
out.println("sOldFileName:"+sOldFileName);
StringsWebRootPath=request.getRealPath("/");//失掉你的web使用的根。
StringsPath=sWebRootPath+"attach";
intiFileCount=0;
StringsServerFileName="";
StringsLocalFileName="";
//文件猎取
if((files!=null)||(!files.isEmpty())){
iFileCount=files.size();
UploadFilefile=(UploadFile)files.get("attach");
sLocalFileName=file.getFileName();
out.println("sLocalFileName:"+sLocalFileName);
intii=sLocalFileName.indexOf(".");//取文件名的后缀
StringsExt=sLocalFileName.substring(ii,sLocalFileName.length());
//失掉不反复的文件名
java.util.Datedt=newjava.util.Date(System.currentTimeMillis());
SimpleDateFormatfmt=newSimpleDateFormat("yyyyMMddHHmmssSSS");
sServerFileName=fmt.format(dt);
sServerFileName=sServerFileName+sExt;
//假如不存在该目次,则新建一个
Filedir=newFile(sPath);
if(!dir.exists()){
dir.mkdirs();
}
upBean.setFolderstore(sPath);//设置要上传的目次
upBean.addUploadListener(fileMover);//增添filMover监听
fileMover.setNewfilename(sServerFileName);//设置服务器上的文件名
upBean.store(mrequest,"attach");//上传
out.println("filepathis"+sPath+"/"+sServerFileName);
}
%>
Demo注重事项:cos.jar,uploadbean.jar,filemover.jar这几个包必需有。
必备下载地点:
UploadBean1.5:http://wcarchive.cdrom.com/pub/simtelnet/winnt/java/uploadbean1_5.zip
FileMover1.3:http://www.javazoom.net/jzservlets/uploadbean/addons/filemover1.3.zip
参考下载地点:
ChinesUpload例子:http://www.javazoom.net/jzservlets/uploadbean/addons/ChineseUpload.zip
参考了fbysss的一篇文章
2.org.apache.struts.upload.FormFile
Struts1.1的org.apache.struts.upload.FormFile类。很便利,不必本人写。也不必写一个jsp挪用jspsmartupload就能够弄定。
选择上传文件页面:selfile.jsp
--------------------------------------------------------------------------------
<%@tagliburi="/WEB-INF/struts-html.tld"prefix="html"%>
<html:html>
<html:formaction="/uploadsAction.do"enctype="multipart/form-data">
<html:fileproperty="theFile"/>
<html:submit/>
</html:form>
</html:html>
--------------------------------------------------------------------------------
UpLoadAction.java
--------------------------------------------------------------------------------
importjava.io.*;
importjavax.servlet.http.*;
importorg.apache.struts.action.*;
importorg.apache.struts.upload.FormFile;
/**
*<p>Title:UpLoadAction</p>
*<p>Description:QRRSMMS</p>
*<p>Copyright:Copyright(c)2004jiahansoft</p>
*<p>Company:jiahansoft</p>
*@authorwanghw
*@version1.0
*/
publicclassUpLoadActionextendsAction{
publicActionForwardexecute(ActionMappingmapping,
ActionFormform,
HttpServletRequestrequest,
HttpServletResponseresponse)
throwsException{
if(forminstanceofuploadsForm){//假如form是uploadsForm
Stringencoding=request.getCharacterEncoding();
if((encoding!=null)&&(encoding.equalsIgnoreCase("utf-8")))
{
response.setContentType("text/html;charset=gb2312");//假如没有指定编码,编码格局为gb2312
}
UpLoadFormtheForm=(UpLoadForm)form;
FormFilefile=theForm.getTheFile();//获得上传的文件
try{
InputStreamstream=file.getInputStream();//把文件读进
StringfilePath=request.getRealPath("/");//取以后体系路径
ByteArrayOutputStreambaos=newByteArrayOutputStream();
OutputStreambos=newFileOutputStream(filePath+"/"+
file.getFileName());//创建一个上传文件的输入流
//System.out.println(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();
}catch(Exceptione){
System.err.print(e);
}
//request.setAttribute("dat",file.getFileName());
returnmapping.findForward("display");
}
returnnull;
}
}
--------------------------------------------------------------------------------
UpLoadForm.java
--------------------------------------------------------------------------------
importjavax.servlet.http.HttpServletRequest;
importorg.apache.struts.action.*;
importorg.apache.struts.upload.*;
/**
*<p>Title:UpLoadForm</p>
*<p>Description:QRRSMMS</p>
*<p>Copyright:Copyright(c)2004jiahansoft</p>
*<p>Company:jiahansoft</p>
*@authorwanghw
*@version1.0
*/
publicclassUpLoadFormextendsActionForm{
publicstaticfinalStringERROR_PROPERTY_MAX_LENGTH_EXCEEDED="org.apache.struts.webapp.upload.MaxLengthExceeded";
protectedFormFiletheFile;
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;
}
}
//这是绝对应的form,另有其他属性能够设置,详细能够参考struts的上传例子。
--------------------------------------------------------------------------------
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>
<form-beans>
<form-beanname="uploadsForm"type="UpLoadForm"/>
</form-beans>
<action-mappings>
<actionname="uploadsForm"type="UpLoadAction"path="/uploadsAction">
<forwardname="display"path="/display.jsp"/>
</action>
</action-mappings>
</struts-config>
<!--display.jsp就是任意写一个乐成页-->
再说说缺点:首先java功能强大的背后是其复杂性,就拿web来说,当今流行的框架有很多,什么struts,spring,jQuery等等,而这无疑增加了java的复杂性。 |
|