JAVA批量传文件,JAVA下传批量上载文件

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);// 根目录upload

if (!f0.exists()) {

f0.mkdir();

}

dir = dir + "/" + article;// 论述题目录article

File 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/企业所在省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';");

}

下载:

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(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 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";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值