資源描述:
《fileupload上傳下載》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在教育資源-天天文庫。
1、Fileupload使用上傳:try{//CheckthatwehaveafileuploadrequestbooleanisMultipart=ServletFileUpload.isMultipartContent(request);if(isMultipart){//Createafactoryfordisk-basedfileitemsFileItemFactoryfactory=newDiskFileItemFactory();//CreateanewfileuploadhandlerServletFil
2、eUploadupload=newServletFileUpload(factory);//ParsetherequestList/*FileItem*/items=upload.parseRequest(request);for(FileItemfileItem:items){if(!fileItem.isFormField()){System.out.println(fileItem.getFieldName());System.out.println(fileItem.getName())
3、;System.out.println(fileItem.getSize());//System.out.println();//getrealpathoftomactserver.Stringpath=getServletContext().getRealPath("\");//System.out.println(path);FilenewFile=newFile(path+"upfile\"+fileItem.getName());OutputStreamoutput=null;try{output=ne
4、wFileOutputStream(newFile);output.write(fileItem.get());output.flush();}catch(Exceptione){e.printStackTrace();}finally{if(output!=null){output.close();}}UpFileDAOupfileDAO=newUpFileDAO();UpFileVOupfile=newUpFileVO();upfile.setFileName(fileItem.getName());upfil
5、e.setFileSize(fileItem.getSize());upfile.setAccess(0);if(!upfileDAO.insertUpFile(upfile)){newFile.delete();}}else{//System.out.println();if("expireTime".equals(fileItem.getFieldName())){System.out.println(fileItem.getString());}}}}}catch(FileUploadExceptione){
6、//TODOAuto-generatedcatchblocke.printStackTrace();}下載:StringfileName=request.getParameter("fileName");Stringpath=getServletContext().getRealPath("\");//System.out.println(path);FilenewFile=newFile(path+"upfile\"+fileName);if(newFile.exists()){InputStreaminpu
7、t=null;OutputStreamoutput=null;try{input=newFileInputStream(newFile);byte[]tempBytes=newbyte[input.available()];input.read(tempBytes);response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition","attachment;filename="+fileName);
8、output=response.getOutputStream();output.write(tempBytes);output.flush();}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{if(input!=null){input.close()