欢迎来到天天文库
浏览记录
ID:40525198
大小:30.50 KB
页数:3页
时间:2019-08-04
《fileupload上传下载》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
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()
此文档下载收益归作者所有