- 论坛徽章:
- 0
|
主要用到的包
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
上传,下载,删除- /**
- * 上传文件到临时目录
- * {模块名}\{日期}\{用户id}\{文件名}
- *
- * @param model
- * @param is
- * @param header
- * @return
- * @throws IOException
- */
- @POST
- @Path("{model}")
- @Consumes(MediaType.MULTIPART_FORM_DATA)
- public String uploadFile(@PathParam("model") String model,
- @FormDataParam("file") InputStream is,
- @FormDataParam("file") FormDataContentDisposition header) throws IOException {
- Integer createEmpId = 99001; //todo 当前登录人ID
- String fileName = new String(header.getFileName().getBytes("ISO8859_1"), "utf-8");
- String afterPath = model + "/" + datedir + "/" + createEmpId.toString() + "/" + fileName;
- String path = tempdir + afterPath;
- File file = new File(path);
- FileUtils.forceMkdir(file.getParentFile());
- file.createNewFile();
- OutputStream out = new FileOutputStream(file);
- try {
- ByteStreams.copy(is, out);
- } finally {
- IOUtils.closeQuietly(is);
- IOUtils.closeQuietly(out);
- }
- //moveFile(model, header.getFileName(), "project", 1);
- return fileName;
- }
-
- /**
- * 批量上传
- *
- * @param model
- * @param form
- * @return list
- * @throws IOException
- */
- @POST
- @Path("/more/{model}")
- @Consumes(MediaType.MULTIPART_FORM_DATA)
- public String uploadMoreFile(@PathParam("model") String model, FormDataMultiPart form) throws IOException {
- String str = "";
- List<FormDataBodyPart> l = form.getFields("file");
- for (FormDataBodyPart p : l) {
- InputStream is = p.getValueAs(InputStream.class);
- FormDataContentDisposition detail = p.getFormDataContentDisposition();
- str += uploadFile(model, is, detail) + "/";
- }
- return str;
- }
- /**
- * 根据文件id下载文件
- */
- @GET
- @Path("download/{id}")
- @Produces(MediaType.APPLICATION_OCTET_STREAM)
- public Response download(@NotNull @PathParam("id") Integer id) {
- Integer createEmpId = 99001; //todo 当前登录人ID
- ZGJAttachment attachment = ZGJAttachment.withFinder().byId(id);
- if (attachment != null && attachment.createEmpid.equals(createEmpId)) {
- String path = attachment.storePath;
- return Response.ok(new File(attachments + path))
- .header("Content-Disposition", "attachment;filename=" + attachment.fileName)
- //.type(DefaultMediaTypePredictor.CommonMediaTypes.getMediaTypeFromFileName(path))
- .build();
- } else return null;
- }
-
- /**
- * 删除临时目录中 最后更新日期为系统日期前一天的所有内容
- */
-
- public void deleteTemplFilesBeforeToday() {
- Akka.system().scheduler().scheduleOnce(Duration.Zero(),
- new Runnable() {
- public void run() {
- File tempFile = new File(tempdir);
- Collection<File> files = FileUtils.listFilesAndDirs(tempFile, new IOFileFilter() {
- public boolean accept(File dir) {
- return true;
- }
-
- public boolean accept(File dir, String name) {
- File f = new File(dir, name);
- if (f.isDirectory())
- return true;
- else
- return false;
- }
- }, new IOFileFilter() {
- public boolean accept(File dir) {
- return dir.isDirectory();
- }
-
- public boolean accept(File dir, String name) {
- File f = new File(dir, name);
- if (f.isDirectory())
- return true;
- else
- return false;
- }
- });
- Calendar cldr = Calendar.getInstance();
- cldr.add(Calendar.DATE, -1);
- long yesterday = cldr.getTime().getTime();
- String yesterdayDate = new SimpleDateFormat("yyyyMMdd").format(yesterday);
- for (File f : files) {
- if (f.exists()) {
- if (f.isFile() && FileUtils.isFileOlder(f, yesterday))
- FileUtils.deleteQuietly(f);
- else if (f.isDirectory() && f.getName().equals(yesterdayDate)) {
- try {
- FileUtils.deleteDirectory(f);
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else logger().debug("删除临时文件未知错误!");
- }
- }
- }
- }, Akka.system().dispatcher());
- }
复制代码 |
|