|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
诸如RMI,EJB等一些技术并不是你说的那么复杂,而是它们把一些复杂的工具封装成不复杂的工具了,理解这些工具是需要些时间。我问你,.net里有这些工具吗?要简单多少?。web|上传
publicclassUploadServletextendsHttpServlet<br>
{<br>
//defaultmaximumallowablefilesizeis100k<br>
staticfinalintMAX_SIZE=102400;<br>
//instancevariablestostorerootandsuccessmessage<br>
StringrootPath,successMessage;<br>
/**<br>
*initmethodiscalledwhenservletisinitialized.<br>
*/<br>
publicvoidinit(ServletConfigconfig)throwsServletException<br>
{<br>
super.init(config);<br>
//getpathinwhichtosavefile<br>
rootPath=config.getInitParameter("RootPath");<br>
if(rootPath==null)<br>
{<br>
rootPath="/";<br>
}<br>
/*Getmessagetoshowwhenuploadiscomplete.Usedonlyif<br>
asuccessredirectpageisnotsupplied.*/<br>
successMessage=config.getInitParameter("SuccessMessage");<br>
if(successMessage==null)<br>
{<br>
successMessage="Fileuploadcomplete!";<br>
}<br>
}<br>
/**<br>
*doPostreadstheuploadeddatafromtherequestandwrites<br>
*ittoafile.<br>
*/<br>
publicvoiddoPost(HttpServletRequestrequest,<br>
HttpServletResponseresponse)<br>
{<br>
ServletOutputStreamout=null;<br>
DataInputStreamin=null;<br>
FileOutputStreamfileOut=null;<br>
try<br>
{<br>
/*setcontenttypeofresponseandgethandletooutput<br>
streamincaseweareunabletoredirectclient*/<br>
response.setContentType("text/plain");<br>
out=response.getOutputStream();<br>
}<br>
catch(IOExceptione)<br>
{<br>
//printerrormessagetostandardout<br>
System.out.println("Errorgettingoutputstream.");<br>
System.out.println("Errordescription:"+e);<br>
return;<br>
}<br>
try<br>
{<br>
//getcontenttypeofclientrequest<br>
StringcontentType=request.getContentType();<br>
//makesurecontenttypeismultipart/form-data<br>
if(contentType!=null&&contentType.indexOf(<br>
"multipart/form-data")!=-1)<br>
{<br>
//openinputstreamfromclienttocaptureuploadfile<br>
in=newDataInputStream(request.getInputStream());<br>
//getlengthofcontentdata<br>
intformDataLength=request.getContentLength();<br>
//allocateabytearraytostorecontentdata<br>
bytedataBytes[]=newbyte[formDataLength];<br>
//readfileintobytearray<br>
intbytesRead=0;<br>
inttotalBytesRead=0;<br>
intsizeCheck=0;<br>
while(totalBytesRead<formDataLength)<br>
{<br>
//checkformaximumfilesizeviolation<br>
sizeCheck=totalBytesRead+in.available();<br>
if(sizeCheck>MAX_SIZE)<br>
{<br>
out.println("Sorry,fileistoolargetoupload.");<br>
return;<br>
}<br>
bytesRead=in.read(dataBytes,totalBytesRead,<br>
formDataLength);<br>
totalBytesRead+=bytesRead;<br>
}<br>
//createstringfrombytearrayforeasymanipulation<br>
Stringfile=newString(dataBytes);<br>
//sincebytearrayisstoredinstring,releasememory<br>
dataBytes=null;<br>
/*getboundaryvalue(boundaryisauniquestringthat<br>
separatescontentdata)*/<br>
intlastIndex=contentType.lastIndexOf("=");<br>
Stringboundary=contentType.substring(lastIndex+1,<br>
contentType.length());<br>
//getDirectorywebvariablefromrequest<br>
Stringdirectory="";<br>
if(file.indexOf("name="Directory"")>0)<br>
{<br>
directory=file.substring(<br>
file.indexOf("name="Directory""));<br>
//removecarriagereturn<br>
directory=directory.substring(<br>
directory.indexOf("
")+1);<br>
//removecarriagereturn<br>
directory=directory.substring(<br>
directory.indexOf("
")+1);<br>
//getDirectory<br>
directory=directory.substring(0,<br>
directory.indexOf("
")-1);<br>
/*makesureuserdidntselectadirectoryhigherin<br>
thedirectorytree*/<br>
if(directory.indexOf("..")>0)<br>
{<br>
out.println("SecurityError:Youcantupload"+<br>
"toadirectoryhigherinthedirectorytree.");<br>
return;<br>
}<br>
}<br>
//getSuccessPagewebvariablefromrequest<br>
StringsuccessPage="";<br>
if(file.indexOf("name="SuccessPage"")>0)<br>
{<br>
successPage=file.substring(<br>
file.indexOf("name="SuccessPage""));<br>
//removecarriagereturn<br>
successPage=successPage.substring(<br>
successPage.indexOf("
")+1);<br>
//removecarriagereturn<br>
successPage=successPage.substring(<br>
successPage.indexOf("
")+1);<br>
//getsuccesspage<br>
successPage=successPage.substring(0,<br>
successPage.indexOf("
")-1);<br>
}<br>
//getOverWriteflagwebvariablefromrequest<br>
StringoverWrite;<br>
if(file.indexOf("name="OverWrite"")>0)<br>
{<br>
overWrite=file.substring(<br>
file.indexOf("name="OverWrite""));<br>
//removecarriagereturn<br>
overWrite=overWrite.substring(<br>
overWrite.indexOf("
")+1);<br>
//removecarriagereturn<br>
overWrite=overWrite.substring(<br>
overWrite.indexOf("
")+1);<br>
//getoverwriteflag<br>
overWrite=overWrite.substring(0,<br>
overWrite.indexOf("
")-1);<br>
}<br>
else<br>
{<br>
overWrite="false";<br>
}<br>
//getOverWritePagewebvariablefromrequest<br>
StringoverWritePage="";<br>
if(file.indexOf("name="OverWritePage"")>0)<br>
{<br>
overWritePage=file.substring(<br>
file.indexOf("name="OverWritePage""));<br>
//removecarriagereturn<br>
overWritePage=overWritePage.substring(<br>
overWritePage.indexOf("
")+1);<br>
//removecarriagereturn<br>
overWritePage=overWritePage.substring(<br>
overWritePage.indexOf("
")+1);<br>
//getoverwritepage<br>
overWritePage=overWritePage.substring(0,<br>
overWritePage.indexOf("
")-1);<br>
}<br>
//getfilenameofuploadfile<br>
StringsaveFile=file.substring(<br>
file.indexOf("filename="")+10);<br>
saveFile=saveFile.substring(0,<br>
saveFile.indexOf("
"));<br>
saveFile=saveFile.substring(<br>
saveFile.lastIndexOf("")+1,<br>
saveFile.indexOf("""));<br>
/*removeboundarymarkersandothermultipart/form-data<br>
tagsfrombeginningofuploadfilesection*/<br>
intpos;//positioninuploadfile<br>
//findpositionofuploadfilesectionofrequest<br>
pos=file.indexOf("filename="");<br>
//findpositionofcontent-dispositionline<br>
pos=file.indexOf("
",pos)+1;<br>
//findpositionofcontent-typeline<br>
pos=file.indexOf("
",pos)+1;<br>
//findpositionofblankline<br>
pos=file.indexOf("
",pos)+1;<br>
/*findthelocationofthenextboundarymarker<br>
(markingtheendoftheuploadfiledata)*/<br>
intboundaryLocation=file.indexOf(boundary,pos)-4;<br>
//uploadfileliesbetweenposandboundaryLocation<br>
file=file.substring(pos,boundaryLocation);<br>
//buildthefullpathoftheuploadfile<br>
StringfileName=newString(rootPath+directory+<br>
saveFile);<br>
//createFileobjecttocheckforexistenceoffile<br>
FilecheckFile=newFile(fileName);<br>
if(checkFile.exists())<br>
{<br>
/*fileexists,ifOverWriteflagisoff,give<br>
messageandabort*/<br>
if(!overWrite.toLowerCase().equals("true"))<br>
{<br>
if(overWritePage.equals(""))<br>
{<br>
/*OverWriteHTMLpageURLnotreceived,respond<br>
withgenericmessage*/<br>
out.println("Sorry,filealreadyexists.");<br>
}<br>
else<br>
{<br>
//redirectclienttoOverWriteHTMLpage<br>
response.sendRedirect(overWritePage);<br>
}<br>
return;<br>
}<br>
}<br>
/*createFileobjecttocheckforexistenceof<br>
Directory*/<br>
FilefileDir=newFile(rootPath+directory);<br>
if(!fileDir.exists())<br>
{<br>
//Directorydoesntexist,createit<br>
fileDir.mkdirs();<br>
}<br>
//instantiatefileoutputstream<br>
fileOut=newFileOutputStream(fileName);<br>
//writethestringtothefileasabytearray<br>
fileOut.write(file.getBytes(),0,file.length());<br>
if(successPage.equals(""))<br>
{<br>
/*successHTMLpageURLnotreceived,respondwith<br>
genericsuccessmessage*/<br>
out.println(successMessage);<br>
out.println("Filewrittento:"+fileName);<br>
}<br>
else<br>
{<br>
//redirectclienttosuccessHTMLpage<br>
response.sendRedirect(successPage);<br>
}<br>
}<br>
else//requestisnotmultipart/form-data<br>
{<br>
//senderrormessagetoclient<br>
out.println("Requestnotmultipart/form-data.");<br>
}<br>
}<br>
catch(Exceptione)<br>
{<br>
try<br>
{<br>
//printerrormessagetostandardout<br>
System.out.println("ErrorindoPost:"+e);<br>
//senderrormessagetoclient<br>
out.println("Anunexpectederrorhasoccurred.");<br>
out.println("Errordescription:"+e);<br>
}<br>
catch(Exceptionf){}<br>
}<br>
finally<br>
{<br>
try<br>
{<br>
fileOut.close();//closefileoutputstream<br>
}<br>
catch(Exceptionf){}<br>
try<br>
{<br>
in.close();//closeinputstreamfromclient<br>
}<br>
catch(Exceptionf){}<br>
try<br>
{<br>
out.close();//closeoutputstreamtoclient<br>
}<br>
catch(Exceptionf){}<br>
}<br>
}<br>
}<br>
<br>
<br>
ruby里有这些工具吗?又要简单多少?我没有用过这两门语言,我估计在这些语言力没有很统一的这种标准,或者根本就没有提供。 |
|