JAVA上传批量下载文件 (二)

2014-11-24 09:12:26 · 作者: · 浏览: 1
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/企业所在省CODE
String photoURL = basePath + fullpath + "/" + bzzStudent.getRegNo() + prefix;// 文件名为
// 学号+后缀
peBzzExamScore.setArticlePath(photoURL);
this.getGeneralService().save(peBzzExamScore);
request().setAttribute("js", "alert('您的论述题已成功上传!');window.location='/entity/workspaceStudent/bzzstudent_aboutArticle.action';");
}
下载:

[java]
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);// 根目录upload
if (!f0.exists()) {
request().setAttribute("js", String.format(m, "根目录"));

return "message";
}
dir = dir + "/" + article;// 论述题目录article
File 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(Restr