首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

JAVA下传批量上载文件

2012-09-10 
JAVA上传批量下载文件笔记记录在此。上传:目录结构:论述题ID/批次ID/省份ID/文件String basePath request

JAVA上传批量下载文件

笔记记录在此。

上传:目录结构:论述题ID/批次ID/省份ID/文件

String basePath = request().getContextPath();basePath = request().getScheme() + "://" + request().getServerName() + ":" + request().getServerPort() + basePath + "/";String realPath = request().getSession().getServletContext().getRealPath(""); // 获取服务器真实路径地址String root = "upload";String article = "article";String dir = ServletActionContext.getServletContext().getRealPath("/" + root);dir = dir.replaceAll("\\\\", "/");File f0 = new File(dir);// 根目录uploadif (!f0.exists()) {f0.mkdir();}dir = dir + "/" + article;// 论述题目录articleFile f1 = new File(dir);if (!f1.exists()) {f1.mkdir();}dir = dir + "/" + peExamBatch.getId();// 当前批次目录File f2 = new File(dir);if (!f2.exists()) {f2.mkdir();}String code = bzzStudent.getPeEnterprise().getEnumConstByFlagSheng().getId();dir = dir + "/" + code;// 省份目录File f3 = new File(dir);if (!f3.exists()) {f3.mkdir();}dir = dir + "/" + bzzStudent.getRegNo() + prefix;if (getUpload() != null && !"".equals(getUpload().getPath())) {FileInputStream fis = new FileInputStream(getUpload());// 其实这里可以判断articlePath文件是否存在,如果存在直接存放而省去取批次,省以及学号等的数据库操作.安全起见FileOutputStream fos = new FileOutputStream(dir);int i = 0;byte[] buf = new byte[1024];while ((i = fis.read(buf)) != -1) {fos.write(buf, 0, i);}fos.flush();fos.close();String fullpath = root + "/" + article + "/" + peExamBatch.getId() + "/" + code;// upload/article/当前批次ID/企业所在省CODEString photoURL = basePath + fullpath + "/" + bzzStudent.getRegNo() + prefix;// 文件名为// 学号+后缀peBzzExamScore.setArticlePath(photoURL);this.getGeneralService().save(peBzzExamScore);request().setAttribute("js", "alert('您的论述题已成功上传!');window.location='/entity/workspaceStudent/bzzstudent_aboutArticle.action';");}


下载:

 

String root = "upload";String article = "article";String m = "alert('当前%s下还没有论述题');";String dir = ServletActionContext.getServletContext().getRealPath("/" + root);dir = dir.replaceAll("\\\\", "/");String upload = dir;// 记录下upload目录File f0 = new File(dir);// 根目录uploadif (!f0.exists()) {request().setAttribute("js", String.format(m, "根目录"));return "message";}dir = dir + "/" + article;// 论述题目录articleFile f1 = new File(dir);if (!f1.exists()) {request().setAttribute("js", String.format(m, "论述题目录"));return "message";}dir = dir + "/" + peBzzExamBatch.getId();// 当前批次目录File f2 = new File(dir);if (!f2.exists()) {request().setAttribute("js", String.format(m, "批次"));return "message";}String zip = dir + "/zip";// 临时目录用于存放压缩后文件目录String srctemp = dir + "/srctemp";// 临时目录用于存放要压缩文件的目录String provid = request().getParameter("prov");if (StringUtils.isEmpty(provid)) {request().setAttribute("js", "alert('请先选择省份!');");return "message";}dir = dir + "/" + provid;// 省份File f3 = new File(dir);if (!f3.exists()) {request().setAttribute("js", String.format(m, "省份"));return "message";}File f4 = new File(zip);if (!f4.exists()) {f4.mkdir();}String enterpriseid = request().getParameter("enterprise");FileManage filemanage = FileManageFactory.creat();try {if (StringUtils.isNotEmpty(enterpriseid) && !"-1".equals(enterpriseid)) {// 如果选择了一级工作单位DetachedCriteria score = DetachedCriteria.forClass(PeBzzExamScore.class);DetachedCriteria student = score.createCriteria("peBzzStudent", "peBzzStudent");DetachedCriteria pe = student.createCriteria("peEnterprise", "peEnterprise");student.add(Restrictions.or(Restrictions.eq("peBzzStudent.peEnterprise.id", enterpriseid), Restrictions.eq("peEnterprise.peEnterprise.id", enterpriseid)));score.createCriteria("enumConstByFlagExamScoreStatus", "enumConstByFlagExamScoreStatus");score.add(Restrictions.eq("enumConstByFlagExamScoreStatus.id", "402880a9aaadc115061dadfcf26b0003"));// 已审核String enterpriseid2 = request().getParameter("enterprise2");if (StringUtils.isNotEmpty(enterpriseid2) && !"-1".equals(enterpriseid2)) {// 如果选择了二级工作单位pe.add(Restrictions.eq("peEnterprise.id", enterpriseid2));}List<PeBzzExamScore> scores = this.getGeneralService().getDetachList(score);if (scores != null && scores.size() == 0) {request().setAttribute("js", "alert('当前条件下没有论述题');");return "message";}File f5 = new File(srctemp);if (!f5.exists()) {f5.mkdir();}dir = srctemp;for (PeBzzExamScore s : scores) {String path = s.getArticlePath();if (StringUtils.isNotEmpty(path)) {int index = path.indexOf("/article");if (index > 0) {String str = path.substring(index);filemanage.copyFile(upload + str, srctemp);// 复制文件}}}}String targetFile = zip + "/" + provid + ".zip";if (filemanage.isExist(targetFile)) {filemanage.delete(targetFile);}if (!filemanage.getSize(dir).equals("0B")) {filemanage.zip(dir, targetFile);// 压缩至指定文件夹String basePath = request().getContextPath();basePath = request().getScheme() + "://" + request().getServerName() + ":" + request().getServerPort() + basePath + "/";int index = targetFile.indexOf("upload/");if (index > 0) {String str = targetFile.substring(index);// request().setAttribute("js",// String.format("window.open('%s');", basePath+str));request().setAttribute("msg", "下载页面");request().setAttribute("js", String.format("window.location.href='%s';", basePath + str));return "message";}} else {request().setAttribute("js", "alert('当前条件下没有论述题');");return "message";}} catch (Exception e) {request().setAttribute("msg", "导出失败!");e.printStackTrace();}return "message";


 

热点排行