zoukankan      html  css  js  c++  java
  • jfinal文件上传与下载

    import com.alibaba.fastjson.JSONObject;
    import com.jfinal.core.Controller;
    import com.jfinal.ext.kit.ZipUtil;
    import com.jfinal.kit.PathKit;
    import com.jfinal.upload.UploadFile;
     
    import java.io.*;
    import java.text.SimpleDateFormat;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Date;
    import java.util.List;
     
    public class FileController extends Controller{
        public void upload() {
            String path = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
            UploadFile file = getFile("imgFile", PathKit.getWebRootPath() + "/temp");
            File source = file.getFile();
            String fileName = file.getFileName();
            String extension = fileName.substring(fileName.lastIndexOf("."));
            String prefix;
            if(".png".equals(extension) || ".jpg".equals(extension) || ".gif".equals(extension)){
                prefix = "img";
                fileName = generateWord() + extension;
            }else{
                prefix = "file";
            }
            JSONObject json = new JSONObject();
            try {
                FileInputStream fis = new FileInputStream(source);
                File targetDir = new File(PathKit.getWebRootPath() + "/" + prefix + "/u/"
                        + path);
                if (!targetDir.exists()) {
                    targetDir.mkdirs();
                }
                File target = new File(targetDir, fileName);
                if (!target.exists()) {
                    target.createNewFile();
                }
                FileOutputStream fos = new FileOutputStream(target);
                byte[] bts = new byte[300];
                while (fis.read(bts, 0, 300) != -1) {
                    fos.write(bts, 0, 300);
                }
                fos.close();
                fis.close();
                json.put("error", 0);
                json.put("url", "/" + prefix + "/u/" + path + "/" + fileName);
                source.delete();
            } catch (FileNotFoundException e) {
                json.put("error", 1);
                json.put("message", "上传出现错误,请稍后再上传");
            } catch (IOException e) {
                json.put("error", 1);
                json.put("message", "文件写入服务器出现错误,请稍后再上传");
            }
            renderJson(json.toJSONString());
        }
     
        public void download(){
            String path = getPara(0);
            String img = PathKit.getWebRootPath() + "/img/u/" + path.replaceAll("_", "/");
            ZipUtil.zip(img, PathKit.getWebRootPath() + "/img/temp/" + path);
            renderFile("/img/temp/" + path + ".zip");
        }
     
        private String generateWord() {
            String[] beforeShuffle = new String[] { "2", "3", "4", "5", "6", "7",
                    "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
                    "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                    "W", "X", "Y", "Z" };
            List<String> list = Arrays.asList(beforeShuffle);
            Collections.shuffle(list);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                sb.append(list.get(i));
            }
            String afterShuffle = sb.toString();
            String result = afterShuffle.substring(5, 9);
            return result;
        }
     
     
    http://www.oschina.net/code/snippet_2255303_43121   //原文
  • 相关阅读:
    Mysql日期函数,时间函数使用的总结
    java与.net比较学习系列(7) 属性
    java与.net比较学习系列(6) 数组
    java与.net比较学习系列(5) 流程控制语句
    java与.net比较学习系列(4) 运算符和表达式
    java与.net比较学习系列(3) 基本数据类型和类型转换
    java与.net比较学习系列(2) 基础语言要素
    java与.net比较学习系列(1) 开发环境和常用调试技巧
    一个简单的数字处理
    SQLSERVER分页存储过程
  • 原文地址:https://www.cnblogs.com/shaohaixiong/p/5761102.html
Copyright © 2011-2022 走看看