cn.hutool.core.io.IoUtil Java Examples

The following examples show how to use cn.hutool.core.io.IoUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HdfsUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
/**
 * 上传文件
 *
 * @param inputStream 输入流
 * @param targetPath  文件路径
 * @throws IOException
 */
public void uploadFile(@NotNull InputStream inputStream, @NotBlank String targetPath) throws Exception {
    // 从输入流中读取二进制数据
    byte[] bytes = IoUtil.readBytes(inputStream);

    FileSystem fileSystem = null;
    FSDataOutputStream outputStream = null;
    try {
        fileSystem = this.hdfsPool.borrowObject();
        outputStream = fileSystem.create(new Path(targetPath));
        outputStream.write(bytes);
        outputStream.flush();
    } finally {
        IoUtil.close(outputStream);
        if (fileSystem != null) { this.hdfsPool.returnObject(fileSystem); }
    }
}
 
Example #2
Source File: SshService.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 下载文件
 *
 * @param sshModel   实体
 * @param remoteFile 远程文件
 * @param save       文件对象
 * @throws FileNotFoundException io
 * @throws SftpException         sftp
 */
public void download(SshModel sshModel, String remoteFile, File save) throws FileNotFoundException, SftpException {
    Session session = null;
    ChannelSftp channel = null;
    OutputStream output = null;
    try {
        session = getSession(sshModel);
        channel = (ChannelSftp) JschUtil.openChannel(session, ChannelType.SFTP);
        output = new FileOutputStream(save);
        channel.get(remoteFile, output);
    } finally {
        IoUtil.close(output);
        JschUtil.close(channel);
        JschUtil.close(session);
    }
}
 
Example #3
Source File: SshService.java    From Jpom with MIT License 6 votes vote down vote up
private String exec(Session session, Charset charset, String command) throws IOException, JSchException {
    ChannelExec channel = null;
    try {
        channel = (ChannelExec) JschUtil.createChannel(session, ChannelType.EXEC);
        channel.setCommand(command);
        InputStream inputStream = channel.getInputStream();
        InputStream errStream = channel.getErrStream();
        channel.connect();
        // 读取结果
        String result = IoUtil.read(inputStream, charset);
        //
        String error = IoUtil.read(errStream, charset);
        return result + error;
    } finally {
        JschUtil.close(channel);
    }
}
 
Example #4
Source File: InitDb.java    From Jpom with MIT License 6 votes vote down vote up
@PreLoadMethod
private static void init() {
    Setting setting = new Setting();
    setting.set("url", DbConfig.getInstance().getDbUrl());
    setting.set("user", "jpom");
    setting.set("pass", "jpom");
    // 调试模式显示sql 信息
    if (JpomManifest.getInstance().isDebug()) {
        setting.set("showSql", "true");
        setting.set("sqlLevel", "INFO");
        setting.set("showParams", "true");
    }
    DefaultSystemLog.getLog().info("初始化数据中....");
    try {
        // 创建连接
        DSFactory dsFactory = DSFactory.create(setting);
        InputStream inputStream = ResourceUtil.getStream("classpath:/bin/h2-db-v1.sql");
        String sql = IoUtil.read(inputStream, CharsetUtil.CHARSET_UTF_8);
        Db.use(dsFactory.getDataSource()).execute(sql);
        DSFactory.setCurrentDSFactory(dsFactory);
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("初始化数据库失败", e);
        System.exit(0);
    }
}
 
Example #5
Source File: FileUtils.java    From sk-admin with Apache License 2.0 6 votes vote down vote up
/**
 * 导出excel
 */
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
    String tempPath =System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
    File file = new File(tempPath);
    BigExcelWriter writer= ExcelUtil.getBigWriter(file);
    // 一次性写出内容,使用默认样式,强制输出标题
    writer.write(list, true);
    //response为HttpServletResponse对象
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
    //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
    response.setHeader("Content-Disposition","attachment;filename=file.xlsx");
    ServletOutputStream out=response.getOutputStream();
    // 终止后删除临时文件
    file.deleteOnExit();
    writer.flush(out, true);
    //此处记得关闭输出Servlet流
    IoUtil.close(out);
}
 
Example #6
Source File: TopTest.java    From Jpom with MIT License 6 votes vote down vote up
private static String execCommand(String[] command) {
    System.out.println(Arrays.toString(command));
    String result = "error";
    try {
        Process process = Runtime.getRuntime().exec(command);
        InputStream is;
        int wait = process.waitFor();
        if (wait == 0) {
            is = process.getInputStream();
        } else {
            is = process.getErrorStream();
        }
        result = IoUtil.read(is, CharsetUtil.GBK);
        is.close();
        process.destroy();
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
    return result;
}
 
Example #7
Source File: TestJavaTail.java    From Jpom with MIT License 6 votes vote down vote up
public static void test() throws IOException {

//        String path = FileDescriptorTest.class.getClassLoader().getResource("").getPath()+"test.txt";
        File file = new File("D:/ssss/a/tset.log");
        FileOutputStream fileOutputStream = new FileOutputStream(file, true);

        FileDescriptor descriptor = fileOutputStream.getFD();
        FileInputStream nfis = new FileInputStream(descriptor);
        String msg = IoUtil.read(nfis, CharsetUtil.CHARSET_UTF_8);
        System.out.println(msg);
        System.out.println("nfis>>>" + nfis.read());
        FileInputStream sfis = new FileInputStream(descriptor);
        System.out.println("sfis>>>" + sfis.read());
        System.out.println("nfis>>>" + nfis.read());
        nfis.close();
        try {
            System.out.println("sfis>>>" + sfis.read());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("nfis执行异常");
        }
        sfis.close();
    }
 
Example #8
Source File: FileUtil.java    From yshopmall with Apache License 2.0 6 votes vote down vote up
/**
 * 导出excel
 */
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
    String tempPath =System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
    File file = new File(tempPath);
    BigExcelWriter writer= ExcelUtil.getBigWriter(file);
    // 一次性写出内容,使用默认样式,强制输出标题
    writer.write(list, true);
    //response为HttpServletResponse对象
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
    //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
    response.setHeader("Content-Disposition","attachment;filename=file.xlsx");
    ServletOutputStream out=response.getOutputStream();
    // 终止后删除临时文件
    file.deleteOnExit();
    writer.flush(out, true);
    //此处记得关闭输出Servlet流
    IoUtil.close(out);
}
 
Example #9
Source File: HdfsUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
/**
 * 上传文件
 * <p>
 * 如果 {@link File}存在,读取文件内容,并上传到 targetPath
 *
 * @param file       原文件
 * @param targetPath 文件路径
 * @throws IOException
 */
public void uploadFile(@NotNull File file, @NotBlank String targetPath) throws Exception {

    if (file == null || !file.exists()) {
        throw new IOException("file not exists");
    }

    // 从文件中读取二进制数据
    byte[] bytes = IoUtil.readBytes(new FileInputStream(file));

    FileSystem fileSystem = null;
    FSDataOutputStream outputStream = null;
    try {
        fileSystem = this.hdfsPool.borrowObject();
        outputStream = fileSystem.create(new Path(targetPath));
        outputStream.write(bytes);
        outputStream.flush();
    } finally {
        IoUtil.close(outputStream);
        if (fileSystem != null) { this.hdfsPool.returnObject(fileSystem); }
    }
}
 
Example #10
Source File: ScriptProcessBuilder.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 结束执行
 *
 * @param msg 响应的消息
 */
private void end(String msg) {
    if (this.process != null) {
        // windows 中不能正常关闭
        this.process.destroy();
        IoUtil.close(inputStream);
        IoUtil.close(errorInputStream);
    }
    Iterator<Session> iterator = sessions.iterator();
    while (iterator.hasNext()) {
        Session session = iterator.next();
        try {
            SocketSessionUtil.send(session, msg);
        } catch (IOException e) {
            DefaultSystemLog.getLog().error("发送消息失败", e);
        }
        iterator.remove();
    }
    FILE_SCRIPT_PROCESS_BUILDER_CONCURRENT_HASH_MAP.remove(this.scriptFile);
}
 
Example #11
Source File: HdfsUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
/**
 * 复制文件
 *
 * @param sourcePath 原文件路径
 * @param targetPath 目标文件路径
 * @throws Exception
 */
public void copyFile(@NotBlank String sourcePath, @NotBlank String targetPath, int buffSize) throws Exception {
    FileSystem fileSystem = null;
    FSDataInputStream inputStream = null;
    FSDataOutputStream outputStream = null;
    try {
        fileSystem = this.hdfsPool.borrowObject();
        inputStream = fileSystem.open(new Path(sourcePath));
        outputStream = fileSystem.create(new Path(targetPath));
        if (buffSize <= 0) {
            int DEFAULT_BUFFER_SIZE = 1024 * 1024 * 64;
            buffSize = DEFAULT_BUFFER_SIZE;
        }
        IOUtils.copyBytes(inputStream, outputStream, buffSize, false);
    } finally {
        IoUtil.close(outputStream);
        IoUtil.close(inputStream);
        if (fileSystem != null) { this.hdfsPool.returnObject(fileSystem); }
    }
}
 
Example #12
Source File: FileUtil.java    From eladmin with Apache License 2.0 6 votes vote down vote up
/**
 * 导出excel
 */
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
    String tempPath = SYS_TEM_DIR + IdUtil.fastSimpleUUID() + ".xlsx";
    File file = new File(tempPath);
    BigExcelWriter writer = ExcelUtil.getBigWriter(file);
    // 一次性写出内容,使用默认样式,强制输出标题
    writer.write(list, true);
    //response为HttpServletResponse对象
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
    //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
    response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
    ServletOutputStream out = response.getOutputStream();
    // 终止后删除临时文件
    file.deleteOnExit();
    writer.flush(out, true);
    //此处记得关闭输出Servlet流
    IoUtil.close(out);
}
 
Example #13
Source File: AccoutResource.java    From albedo with GNU Lesser General Public License v3.0 6 votes vote down vote up
@AnonymousAccess
@GetMapping(path = "/code/{randomStr}")
@ApiOperation(value = "获取验证码")
public void valicode(@PathVariable String randomStr, HttpServletResponse response) throws IOException {
	Assert.isTrue(StringUtil.isNotEmpty(randomStr), "机器码不能为空");
	response.setHeader("Cache-Control", "no-store, no-cache");
	response.setHeader("Transfer-Encoding", "JPG");
	response.setContentType("image/jpeg");
	//生成文字验证码
	String text = producer.createText();
	//生成图片验证码
	BufferedImage image = producer.createImage(text);
	RedisUtil.setCacheString(CommonConstants.DEFAULT_CODE_KEY + randomStr, text, CommonConstants.DEFAULT_IMAGE_EXPIRE, TimeUnit.SECONDS);
	//创建输出流
	ServletOutputStream out = response.getOutputStream();
	//写入数据
	ImageIO.write(image, "jpeg", out);
	IoUtil.close(out);
}
 
Example #14
Source File: GenUtil.java    From albedo with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * XML文件转换为对象
 *
 * @param fileName
 * @param clazz
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T fileToObject(String fileName, Class<?> clazz) {
	String pathName = getTemplatePath() + fileName;
	logger.debug("file to object: {} ", pathName);

	PathMatchingResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
	String content = null;
	try {
		content = IoUtil.read(resourceLoader.getResources(pathName)[0].getInputStream(), CharsetUtil.CHARSET_UTF_8);
		return (T) JaxbMapper.fromXml(content, clazz);
	} catch (IOException e) {
		logger.warn("error convert: {}", e.getMessage());
	}
	return null;
}
 
Example #15
Source File: RestFileInfoService.java    From Guns with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 获取文件流
 *
 * @author fengshuonan
 * @Date 2019-05-04 17:04
 */
public byte[] getFileBytes(String fileId) {

    if (ToolUtil.isEmpty(fileId)) {
        throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND);
    }

    RestFileInfo fileInfo = this.getById(fileId);
    if (fileInfo == null) {
        throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND);
    } else {
        try {
            String filePath = fileInfo.getFilePath();
            return IoUtil.readBytes(new FileInputStream(filePath));
        } catch (FileNotFoundException e) {
            log.error("文件未找到,id为:" + fileId, e);
            throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND);
        }
    }
}
 
Example #16
Source File: CodeGenServiceTest.java    From spring-boot-demo with MIT License 6 votes vote down vote up
@Test
@SneakyThrows
public void testGeneratorCode() {
    GenConfig config = new GenConfig();

    TableRequest request = new TableRequest();
    request.setPrepend("jdbc:mysql://");
    request.setUrl("127.0.0.1:3306/spring-boot-demo");
    request.setUsername("root");
    request.setPassword("root");
    request.setTableName("shiro_user");
    config.setRequest(request);

    config.setModuleName("shiro");
    config.setAuthor("Yangkai.Shen");
    config.setComments("用户角色信息");
    config.setPackageName("com.xkcoding");
    config.setTablePrefix("shiro_");

    byte[] zip = codeGenService.generatorCode(config);
    OutputStream outputStream = new FileOutputStream(new File("/Users/yangkai.shen/Desktop/" + request.getTableName() + ".zip"));
    IoUtil.write(outputStream, true, zip);
}
 
Example #17
Source File: HdfsUtil.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
/**
 * 读取文件内容
 *
 * @param path 文件路径
 * @return
 * @throws Exception
 */
public String readFile(@NotBlank String path) throws Exception {
    if (!exists(path)) {
        throw new IOException(path + " not exists in hdfs");
    }

    // 目标路径
    Path sourcePath = new Path(path);
    BufferedReader reader = null;
    FSDataInputStream inputStream = null;
    FileSystem fileSystem = null;
    try {
        fileSystem = this.hdfsPool.borrowObject();
        inputStream = fileSystem.open(sourcePath);
        // 防止中文乱码
        reader = new BufferedReader(new InputStreamReader(inputStream));
        String lineTxt = "";
        StringBuffer sb = new StringBuffer();
        while ((lineTxt = reader.readLine()) != null) {
            sb.append(lineTxt);
        }
        return sb.toString();
    } finally {
        IoUtil.close(reader);
        IoUtil.close(inputStream);
        if (fileSystem != null) { this.hdfsPool.returnObject(fileSystem); }
    }
}
 
Example #18
Source File: TestSSh.java    From Jpom with MIT License 5 votes vote down vote up
/**
     * 执行一条命令
     */
    public static void execCmd(String command, Session session) throws Exception {
        Channel channel = JschUtil.createChannel(session, ChannelType.EXEC);
        ((ChannelExec) channel).setCommand(command);
        channel.setInputStream(null);
        ((ChannelExec) channel).setErrStream(System.err);


        System.out.println("执行");
//        IoUtil.readUtf8Lines(channel.getExtInputStream(), new LineHandler() {
//            @Override
//            public void handle(String line) {
//                System.out.println(line);
//            }
//        });
        InputStream inputStream = channel.getInputStream();
        channel.connect();
        IoUtil.readLines(inputStream, CharsetUtil.CHARSET_UTF_8, new LineHandler() {
            @Override
            public void handle(String line) {
                System.out.println(line);
            }
        });
        int exitStatus = channel.getExitStatus();
        System.out.println(exitStatus);
        channel.disconnect();
        session.disconnect();
    }
 
Example #19
Source File: RsaKeyUtil.java    From netty-learning-example with Apache License 2.0 5 votes vote down vote up
/**
 * 生成私钥文件
 */
public static void main(String[] args) {
    System.out.println();
    System.out.print("输入保存密钥文件的路径(如: f:/rsa/): ");
    Scanner scanner = new Scanner(System.in);
    String path = scanner.nextLine();
    KeyPair keyPair = SecureUtil.generateKeyPair("RSA", 512, LocalDateTime.now().toString().getBytes());
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    String privatePath = path + "auth-private.key";
    IoUtil.writeObjects(FileUtil.getOutputStream(privatePath), true, privateKey);
}
 
Example #20
Source File: XssHttpRequestWrapper.java    From kvf-admin with MIT License 5 votes vote down vote up
@Override
public ServletInputStream getInputStream() throws IOException {
    // 非json类型,直接返回
    if (!MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(super.getHeader(HttpHeaders.CONTENT_TYPE))) {
        return super.getInputStream();
    }

    // 为空,直接返回
    String json = IoUtil.read(super.getInputStream(), "utf-8");
    if (StrUtil.isBlank(json)) {
        return super.getInputStream();
    }

    // xss过滤
    json = xssEncode(json);
    final ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
    return new ServletInputStream() {
        @Override
        public boolean isFinished() {
            return true;
        }

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setReadListener(ReadListener readListener) {
        }

        @Override
        public int read() throws IOException {
            return bis.read();
        }
    };
}
 
Example #21
Source File: JpomApplicationEvent.java    From Jpom with MIT License 5 votes vote down vote up
/**
 * 解锁进程文件
 */
private void unLockFile() {
    if (lock != null) {
        try {
            lock.release();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    IoUtil.close(lock);
    IoUtil.close(fileChannel);
    IoUtil.close(fileOutputStream);
}
 
Example #22
Source File: FileInfoService.java    From Guns with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 预览当前用户头像
 *
 * @author fengshuonan
 * @Date 2019-05-04 17:04
 */
public byte[] previewAvatar() {

    LoginUser currentUser = LoginContextHolder.getContext().getUser();
    if (currentUser == null) {
        throw new ServiceException(CoreExceptionEnum.NO_CURRENT_USER);
    }

    //获取当前用户的头像id
    User user = userService.getById(currentUser.getId());
    String avatar = user.getAvatar();

    //如果头像id为空就返回默认的
    if (ToolUtil.isEmpty(avatar)) {
        return Base64.decode(DefaultAvatar.BASE_64_AVATAR);
    } else {

        //文件id不为空就查询文件记录
        FileInfo fileInfo = this.getById(avatar);
        if (fileInfo == null) {
            return Base64.decode(DefaultAvatar.BASE_64_AVATAR);
        } else {
            try {
                String filePath = fileInfo.getFilePath();
                return IoUtil.readBytes(new FileInputStream(filePath));
            } catch (FileNotFoundException e) {
                log.error("头像未找到!", e);
                return Base64.decode(DefaultAvatar.BASE_64_AVATAR);
            }
        }
    }

}
 
Example #23
Source File: CodeGenController.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 生成代码
 */
@SneakyThrows
@PostMapping("")
public void generatorCode(@RequestBody GenConfig genConfig, HttpServletResponse response) {
    byte[] data = codeGenService.generatorCode(genConfig);

    response.reset();
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=%s.zip", genConfig.getTableName()));
    response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length));
    response.setContentType("application/octet-stream; charset=UTF-8");

    IoUtil.write(response.getOutputStream(), Boolean.TRUE, data);
}
 
Example #24
Source File: CodeGenServiceImpl.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 生成代码
 *
 * @param genConfig 生成配置
 * @return 代码压缩文件
 */
@Override
public byte[] generatorCode(GenConfig genConfig) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(outputStream);

    //查询表信息
    Entity table = queryTable(genConfig.getRequest());
    //查询列信息
    List<Entity> columns = queryColumns(genConfig.getRequest());
    //生成代码
    CodeGenUtil.generatorCode(genConfig, table, columns, zip);
    IoUtil.close(zip);
    return outputStream.toByteArray();
}
 
Example #25
Source File: SshService.java    From Jpom with MIT License 5 votes vote down vote up
/**
 * 检查是否存在正在运行的进程
 *
 * @param sshModel ssh
 * @param tag      标识
 * @return true 存在运行中的
 * @throws IOException   IO
 * @throws JSchException jsch
 */
public boolean checkSshRun(SshModel sshModel, String tag) throws IOException, JSchException {
    String ps = StrUtil.format("ps -ef | grep -v 'grep' | egrep {}", tag);
    Session session = null;
    ChannelExec channel = null;
    try {
        session = getSession(sshModel);
        channel = (ChannelExec) JschUtil.createChannel(session, ChannelType.EXEC);
        channel.setCommand(ps);
        InputStream inputStream = channel.getInputStream();
        InputStream errStream = channel.getErrStream();
        channel.connect();
        Charset charset = sshModel.getCharsetT();
        // 运行中
        AtomicBoolean run = new AtomicBoolean(false);
        IoUtil.readLines(inputStream, charset, (LineHandler) s -> {
            run.set(true);
        });
        if (run.get()) {
            return true;
        }
        run.set(false);
        AtomicReference<String> error = new AtomicReference<>();
        IoUtil.readLines(errStream, charset, (LineHandler) s -> {
            run.set(true);
            error.set(s);
        });
        if (run.get()) {
            throw new JpomRuntimeException("检查异常:" + error.get());
        }
    } finally {
        JschUtil.close(channel);
        JschUtil.close(session);
    }
    return false;
}
 
Example #26
Source File: SysGeneratorController.java    From smaker with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 生成代码
 */
@PostMapping("/code")
public void code(@RequestBody GenConfig genConfig, HttpServletResponse response) throws IOException {
	byte[] data = sysGeneratorService.generatorCode(genConfig);

	response.reset();
	response.setHeader("Content-Disposition", String.format("attachment; filename=%s.zip", genConfig.getTableName()));
	response.addHeader("Content-Length", "" + data.length);
	response.setContentType("application/octet-stream; charset=UTF-8");

	IoUtil.write(response.getOutputStream(), Boolean.TRUE, data);
}
 
Example #27
Source File: SysGeneratorServiceImpl.java    From smaker with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 生成代码
 *
 * @param genConfig 生成配置
 * @return
 */
@Override
public byte[] generatorCode(GenConfig genConfig) {
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	ZipOutputStream zip = new ZipOutputStream(outputStream);

	//查询表信息
	Map<String, String> table = queryTable(genConfig.getTableName());
	//查询列信息
	List<Map<String, String>> columns = queryColumns(genConfig.getTableName());
	//生成代码
	GenUtils.generatorCode(genConfig, table, columns, zip);
	IoUtil.close(zip);
	return outputStream.toByteArray();
}
 
Example #28
Source File: ServletUtils.java    From yue-library with Apache License 2.0 5 votes vote down vote up
/**
 * 返回数据给客户端
 * 
 * @param text 返回的内容
 * @param contentType 返回的类型
 */
public static void write(String text, String contentType) {
	HttpServletResponse response = getResponse();
	response.setContentType(contentType);
	Writer writer = null;
	try {
		writer = response.getWriter();
		writer.write(text);
		writer.flush();
	} catch (IOException e) {
		throw new UtilException(e);
	} finally {
		IoUtil.close(writer);
	}
}
 
Example #29
Source File: ServletUtils.java    From yue-library with Apache License 2.0 5 votes vote down vote up
/**
 * 返回文件给客户端
 * 
 * @param file 写出的文件对象
 * @since 4.1.15
 */
public static void write(File file) {
	final String fileName = file.getName();
	final String contentType = ObjectUtil.defaultIfNull(FileUtil.getMimeType(fileName), "application/octet-stream");
	BufferedInputStream in = null;
	try {
		in = FileUtil.getInputStream(file);
		write(in, contentType, fileName);
	} finally {
		IoUtil.close(in);
	}
}
 
Example #30
Source File: ServletUtils.java    From yue-library with Apache License 2.0 5 votes vote down vote up
/**
 * 返回数据给客户端
 * 
 * @param in 需要返回客户端的内容
 * @param bufferSize 缓存大小
 */
public static void write(InputStream in, int bufferSize) {
	ServletOutputStream out = null;
	try {
		out = getResponse().getOutputStream();
		IoUtil.copy(in, out, bufferSize);
	} catch (IOException e) {
		throw new UtilException(e);
	} finally {
		IoUtil.close(out);
		IoUtil.close(in);
	}
}