免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1395 | 回复: 0
打印 上一主题 下一主题

Java上传下载,支持单个、批量 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-24 09:19 |只看该作者 |倒序浏览
主要用到的包
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;

上传,下载,删除
  1. /**
  2.      * 上传文件到临时目录
  3.      * {模块名}\{日期}\{用户id}\{文件名}
  4.      *
  5.      * @param model
  6.      * @param is
  7.      * @param header
  8.      * @return
  9.      * @throws IOException
  10.      */
  11.     @POST
  12.     @Path("{model}")
  13.     @Consumes(MediaType.MULTIPART_FORM_DATA)
  14.     public String uploadFile(@PathParam("model") String model,
  15.                              @FormDataParam("file") InputStream is,
  16.                              @FormDataParam("file") FormDataContentDisposition header) throws IOException {
  17.         Integer createEmpId = 99001; //todo 当前登录人ID
  18.         String fileName = new String(header.getFileName().getBytes("ISO8859_1"), "utf-8");
  19.         String afterPath = model + "/" + datedir + "/" + createEmpId.toString() + "/" + fileName;
  20.         String path = tempdir + afterPath;
  21.         File file = new File(path);
  22.         FileUtils.forceMkdir(file.getParentFile());
  23.         file.createNewFile();
  24.         OutputStream out = new FileOutputStream(file);
  25.         try {
  26.             ByteStreams.copy(is, out);
  27.         } finally {
  28.             IOUtils.closeQuietly(is);
  29.             IOUtils.closeQuietly(out);
  30.         }
  31.         //moveFile(model, header.getFileName(), "project", 1);
  32.         return fileName;
  33.     }

  34.     /**
  35.      * 批量上传
  36.      *
  37.      * @param model
  38.      * @param form
  39.      * @return list
  40.      * @throws IOException
  41.      */
  42.     @POST
  43.     @Path("/more/{model}")
  44.     @Consumes(MediaType.MULTIPART_FORM_DATA)
  45.     public String uploadMoreFile(@PathParam("model") String model, FormDataMultiPart form) throws IOException {
  46.         String str = "";
  47.         List<FormDataBodyPart> l = form.getFields("file");
  48.         for (FormDataBodyPart p : l) {
  49.             InputStream is = p.getValueAs(InputStream.class);
  50.             FormDataContentDisposition detail = p.getFormDataContentDisposition();
  51.             str += uploadFile(model, is, detail) + "/";
  52.         }
  53.         return str;
  54.     }
  55. /**
  56.      * 根据文件id下载文件
  57.      */
  58.     @GET
  59.     @Path("download/{id}")
  60.     @Produces(MediaType.APPLICATION_OCTET_STREAM)
  61.     public Response download(@NotNull @PathParam("id") Integer id) {
  62.         Integer createEmpId = 99001; //todo 当前登录人ID
  63.         ZGJAttachment attachment = ZGJAttachment.withFinder().byId(id);
  64.         if (attachment != null && attachment.createEmpid.equals(createEmpId)) {
  65.             String path = attachment.storePath;
  66.             return Response.ok(new File(attachments + path))
  67.                     .header("Content-Disposition", "attachment;filename=" + attachment.fileName)
  68.                             //.type(DefaultMediaTypePredictor.CommonMediaTypes.getMediaTypeFromFileName(path))
  69.                     .build();
  70.         } else return null;
  71.     }

  72. /**
  73.      * 删除临时目录中 最后更新日期为系统日期前一天的所有内容
  74.      */

  75.     public void deleteTemplFilesBeforeToday() {
  76.         Akka.system().scheduler().scheduleOnce(Duration.Zero(),
  77.                 new Runnable() {
  78.                     public void run() {
  79.                         File tempFile = new File(tempdir);
  80.                         Collection<File> files = FileUtils.listFilesAndDirs(tempFile, new IOFileFilter() {
  81.                             public boolean accept(File dir) {
  82.                                 return true;
  83.                             }

  84.                             public boolean accept(File dir, String name) {
  85.                                 File f = new File(dir, name);
  86.                                 if (f.isDirectory())
  87.                                     return true;
  88.                                 else
  89.                                     return false;
  90.                             }
  91.                         }, new IOFileFilter() {
  92.                             public boolean accept(File dir) {
  93.                                 return dir.isDirectory();
  94.                             }

  95.                             public boolean accept(File dir, String name) {
  96.                                 File f = new File(dir, name);
  97.                                 if (f.isDirectory())
  98.                                     return true;
  99.                                 else
  100.                                     return false;
  101.                             }
  102.                         });
  103.                         Calendar cldr = Calendar.getInstance();
  104.                         cldr.add(Calendar.DATE, -1);
  105.                         long yesterday = cldr.getTime().getTime();
  106.                         String yesterdayDate = new SimpleDateFormat("yyyyMMdd").format(yesterday);
  107.                         for (File f : files) {
  108.                             if (f.exists()) {
  109.                                 if (f.isFile() && FileUtils.isFileOlder(f, yesterday))
  110.                                     FileUtils.deleteQuietly(f);
  111.                                 else if (f.isDirectory() && f.getName().equals(yesterdayDate)) {
  112.                                     try {
  113.                                         FileUtils.deleteDirectory(f);
  114.                                     } catch (IOException e) {
  115.                                         e.printStackTrace();
  116.                                     }
  117.                                 } else logger().debug("删除临时文件未知错误!");
  118.                             }
  119.                         }
  120.                     }
  121.                 }, Akka.system().dispatcher());
  122.     }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP