JAVA上传批量下载文件
笔记记录在此。
上传:目录结构:论述题ID/批次ID/省份ID/文件
[java]
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 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.replace
补充:软件开发 , Java ,