zoukankan      html  css  js  c++  java
  • springclud中附件上传

    package org.springblade.desk.controller;
    
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    import lombok.AllArgsConstructor;
    import org.springblade.common.cache.CacheNames;
    import org.springblade.core.boot.ctrl.BladeController;
    import org.springblade.core.mp.support.Condition;
    import org.springblade.core.mp.support.Query;
    import org.springblade.core.tool.api.R;
    import org.springblade.core.tool.utils.Func;
    import org.springblade.desk.entity.OaAttachment;
    import org.springblade.desk.service.IOaAttachmentService;
    import org.springblade.desk.vo.OaAttachmentVO;
    import org.springblade.desk.wrapper.OaAttachmentWrapper;
    import org.springframework.util.FileCopyUtils;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;
    import springfox.documentation.annotations.ApiIgnore;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;
    
    /**
     * 控制器
     *
     * @author Sxx
     */
    //@ApiIgnore
    @RestController
    @RequestMapping("/oa/attachment")
    @AllArgsConstructor
    @Api(value = "文件", tags = "文件")
    public class OaAttachmentController extends BladeController implements CacheNames {
    
        private IOaAttachmentService oaAttachmentService;
    
        /**
         * 上传文件
         * @param files    文件
         */
        @PostMapping("upload")
        @ApiOperationSupport(order = 1)
        @ApiOperation(value = "上传文件", notes = "传入文件")
        public R<List<OaAttachment>> upload(@RequestParam List<MultipartFile> files) {
            List<OaAttachment> list = new ArrayList<OaAttachment>();
            //String dirPath = getRequest().getServletContext().getRealPath("/") +"uploadFile/";
            String dirPath = "E:/oaAttachment/uploadFile/" ;
            File dir = new File(dirPath);
            if (!dir.exists()) {
                dir.mkdir();
            }
            files.forEach(file -> {
                try {
                    String fileName = file.getOriginalFilename();
                    String fileType = fileName.substring(fileName.indexOf("."));
                    String filePath = UUID.randomUUID().toString() +fileType;
                    File newFile = new File(dirPath,  filePath);
                    FileCopyUtils.copy(file.getInputStream(),Files.newOutputStream(newFile.toPath()));
                    OaAttachment attachment = new OaAttachment();
                    attachment.setName(fileName);
                    attachment.setType(fileType);
                    attachment.setUrl("uploadFile/"+filePath);
                    attachment.setAttachmentSize(file.getSize());
                    oaAttachmentService.save(attachment);
                    list.add(attachment);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            return R.data(list);
        }
    
        /**
         * 详情
         */
        @GetMapping("/detail")
        @ApiOperationSupport(order = 1)
        @ApiOperation(value = "详情", notes = "传入entity")
        public R<OaAttachmentVO> detail(OaAttachment entity) {
            OaAttachment detail = oaAttachmentService.getOne(Condition.getQueryWrapper(entity));
            return R.data(OaAttachmentWrapper.build().entityVO(detail));
        }
    
        /**
         * 列表分页
         */
        @GetMapping("/list")
        @ApiOperationSupport(order = 2)
        @ApiOperation(value = "列表分页", notes = "传入entity")
        public R<IPage<OaAttachmentVO>> list(@ApiIgnore @RequestParam Map<String, Object> entity, Query query) {
            IPage<OaAttachment> pages = oaAttachmentService.page(Condition.getPage(query), Condition.getQueryWrapper(entity, OaAttachment.class));
            return R.data(OaAttachmentWrapper.build().pageVO(pages));
        }
    
        /**
         * 新增
         */
        @PostMapping("/save")
        @ApiOperationSupport(order = 4)
        @ApiOperation(value = "新增", notes = "传入entity")
        public R save(@RequestBody OaAttachment entity) {
            return R.status(oaAttachmentService.save(entity));
        }
    
        /**
         * 修改
         */
        @PostMapping("/update")
        @ApiOperationSupport(order = 5)
        @ApiOperation(value = "修改", notes = "传入entity")
        public R update(@RequestBody OaAttachment entity) {
            return R.status(oaAttachmentService.updateById(entity));
        }
    
        /**
         * 新增或修改
         */
        @PostMapping("/submit")
        @ApiOperationSupport(order = 6)
        @ApiOperation(value = "新增或修改", notes = "传入entity")
        public R submit(@RequestBody OaAttachment entity) {
            return R.status(oaAttachmentService.saveOrUpdate(entity));
        }
    
        /**
         * 删除
         */
        @PostMapping("/remove")
        @ApiOperationSupport(order = 7)
        @ApiOperation(value = "逻辑删除", notes = "传入entity")
        public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
            boolean temp = oaAttachmentService.deleteLogic(Func.toLongList(ids));
            return R.status(temp);
        }
    
    }

    //=======实现类
    /*
    * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are met:
    *
    * Redistributions of source code must retain the above copyright notice,
    * this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
    * notice, this list of conditions and the following disclaimer in the
    * documentation and/or other materials provided with the distribution.
    * Neither the name of the dreamlu.net developer nor the names of its
    * contributors may be used to endorse or promote products derived from
    * this software without specific prior written permission.
    * Author: Chill 庄骞 (smallchill@163.com)
    */
    package org.springblade.desk.entity;

    import com.baomidou.mybatisplus.annotation.TableName;
    import com.fasterxml.jackson.databind.annotation.JsonSerialize;
    import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import org.springblade.core.mp.base.BaseEntity;

    /**
    * OA附件实体类
    *
    * @author Sxx
    */
    @Data
    @TableName("oa_attachment")
    @EqualsAndHashCode(callSuper = true)
    public class OaAttachment extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
    * businessId
    */
    @JsonSerialize(
    using = ToStringSerializer.class
    )
    private Long businessId;

    /**
    * processInstanceId
    */
    private String processInstanceId;

    /**
    * 名称
    */
    private String name;
    /**
    * 地址
    */
    private String url;
    /**
    * 类型
    */
    private String type;
    /**
    * 大小
    */
    private long attachmentSize;

    }
    //==== util
    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //

    package org.springframework.util;

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.Reader;
    import java.io.StringWriter;
    import java.io.Writer;
    import java.nio.file.Files;
    import org.springframework.lang.Nullable;

    public abstract class FileCopyUtils {
    public static final int BUFFER_SIZE = 4096;

    public FileCopyUtils() {
    }

    public static int copy(File in, File out) throws IOException {
    Assert.notNull(in, "No input File specified");
    Assert.notNull(out, "No output File specified");
    return copy(Files.newInputStream(in.toPath()), Files.newOutputStream(out.toPath()));
    }

    public static void copy(byte[] in, File out) throws IOException {
    Assert.notNull(in, "No input byte array specified");
    Assert.notNull(out, "No output File specified");
    copy((InputStream)(new ByteArrayInputStream(in)), (OutputStream)Files.newOutputStream(out.toPath()));
    }

    public static byte[] copyToByteArray(File in) throws IOException {
    Assert.notNull(in, "No input File specified");
    return copyToByteArray(Files.newInputStream(in.toPath()));
    }

    public static int copy(InputStream in, OutputStream out) throws IOException {
    Assert.notNull(in, "No InputStream specified");
    Assert.notNull(out, "No OutputStream specified");

    int var2;
    try {
    var2 = StreamUtils.copy(in, out);
    } finally {
    try {
    in.close();
    } catch (IOException var12) {
    }

    try {
    out.close();
    } catch (IOException var11) {
    }

    }

    return var2;
    }

    public static void copy(byte[] in, OutputStream out) throws IOException {
    Assert.notNull(in, "No input byte array specified");
    Assert.notNull(out, "No OutputStream specified");

    try {
    out.write(in);
    } finally {
    try {
    out.close();
    } catch (IOException var8) {
    }

    }

    }

    public static byte[] copyToByteArray(@Nullable InputStream in) throws IOException {
    if (in == null) {
    return new byte[0];
    } else {
    ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
    copy((InputStream)in, (OutputStream)out);
    return out.toByteArray();
    }
    }

    public static int copy(Reader in, Writer out) throws IOException {
    Assert.notNull(in, "No Reader specified");
    Assert.notNull(out, "No Writer specified");

    try {
    int byteCount = 0;
    char[] buffer = new char[4096];

    int bytesRead;
    for(boolean var4 = true; (bytesRead = in.read(buffer)) != -1; byteCount += bytesRead) {
    out.write(buffer, 0, bytesRead);
    }

    out.flush();
    int var5 = byteCount;
    return var5;
    } finally {
    try {
    in.close();
    } catch (IOException var15) {
    }

    try {
    out.close();
    } catch (IOException var14) {
    }

    }
    }

    public static void copy(String in, Writer out) throws IOException {
    Assert.notNull(in, "No input String specified");
    Assert.notNull(out, "No Writer specified");

    try {
    out.write(in);
    } finally {
    try {
    out.close();
    } catch (IOException var8) {
    }

    }

    }

    public static String copyToString(@Nullable Reader in) throws IOException {
    if (in == null) {
    return "";
    } else {
    StringWriter out = new StringWriter();
    copy((Reader)in, (Writer)out);
    return out.toString();
    }
    }
    }
    // util======
    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //

    package org.springframework.util;

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.FilterInputStream;
    import java.io.FilterOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.nio.charset.Charset;
    import org.springframework.lang.Nullable;

    public abstract class StreamUtils {
    public static final int BUFFER_SIZE = 4096;
    private static final byte[] EMPTY_CONTENT = new byte[0];

    public StreamUtils() {
    }

    public static byte[] copyToByteArray(@Nullable InputStream in) throws IOException {
    if (in == null) {
    return new byte[0];
    } else {
    ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
    copy((InputStream)in, out);
    return out.toByteArray();
    }
    }

    public static String copyToString(@Nullable InputStream in, Charset charset) throws IOException {
    if (in == null) {
    return "";
    } else {
    StringBuilder out = new StringBuilder();
    InputStreamReader reader = new InputStreamReader(in, charset);
    char[] buffer = new char[4096];
    boolean var5 = true;

    int bytesRead;
    while((bytesRead = reader.read(buffer)) != -1) {
    out.append(buffer, 0, bytesRead);
    }

    return out.toString();
    }
    }

    public static void copy(byte[] in, OutputStream out) throws IOException {
    Assert.notNull(in, "No input byte array specified");
    Assert.notNull(out, "No OutputStream specified");
    out.write(in);
    }

    public static void copy(String in, Charset charset, OutputStream out) throws IOException {
    Assert.notNull(in, "No input String specified");
    Assert.notNull(charset, "No charset specified");
    Assert.notNull(out, "No OutputStream specified");
    Writer writer = new OutputStreamWriter(out, charset);
    writer.write(in);
    writer.flush();
    }

    public static int copy(InputStream in, OutputStream out) throws IOException {
    Assert.notNull(in, "No InputStream specified");
    Assert.notNull(out, "No OutputStream specified");
    int byteCount = 0;
    byte[] buffer = new byte[4096];

    int bytesRead;
    for(boolean var4 = true; (bytesRead = in.read(buffer)) != -1; byteCount += bytesRead) {
    out.write(buffer, 0, bytesRead);
    }

    out.flush();
    return byteCount;
    }

    public static long copyRange(InputStream in, OutputStream out, long start, long end) throws IOException {
    Assert.notNull(in, "No InputStream specified");
    Assert.notNull(out, "No OutputStream specified");
    long skipped = in.skip(start);
    if (skipped < start) {
    throw new IOException("Skipped only " + skipped + " bytes out of " + start + " required");
    } else {
    long bytesToCopy = end - start + 1L;
    byte[] buffer = new byte[4096];

    while(bytesToCopy > 0L) {
    int bytesRead = in.read(buffer);
    if (bytesRead == -1) {
    break;
    }

    if ((long)bytesRead <= bytesToCopy) {
    out.write(buffer, 0, bytesRead);
    bytesToCopy -= (long)bytesRead;
    } else {
    out.write(buffer, 0, (int)bytesToCopy);
    bytesToCopy = 0L;
    }
    }

    return end - start + 1L - bytesToCopy;
    }
    }

    public static int drain(InputStream in) throws IOException {
    Assert.notNull(in, "No InputStream specified");
    byte[] buffer = new byte[4096];
    int bytesRead = true;

    int byteCount;
    int bytesRead;
    for(byteCount = 0; (bytesRead = in.read(buffer)) != -1; byteCount += bytesRead) {
    }

    return byteCount;
    }

    public static InputStream emptyInput() {
    return new ByteArrayInputStream(EMPTY_CONTENT);
    }

    public static InputStream nonClosing(InputStream in) {
    Assert.notNull(in, "No InputStream specified");
    return new StreamUtils.NonClosingInputStream(in);
    }

    public static OutputStream nonClosing(OutputStream out) {
    Assert.notNull(out, "No OutputStream specified");
    return new StreamUtils.NonClosingOutputStream(out);
    }

    private static class NonClosingOutputStream extends FilterOutputStream {
    public NonClosingOutputStream(OutputStream out) {
    super(out);
    }

    public void write(byte[] b, int off, int let) throws IOException {
    this.out.write(b, off, let);
    }

    public void close() throws IOException {
    }
    }

    private static class NonClosingInputStream extends FilterInputStream {
    public NonClosingInputStream(InputStream in) {
    super(in);
    }

    public void close() throws IOException {
    }
    }
    }
     
  • 相关阅读:
    GTD时间管理(1)---捕获搜集
    ios面试总结-
    Swift入门篇-结构体
    Swift入门篇-闭包和函数
    swift入门篇-函数
    Swift入门篇-集合
    Swift入门篇-循环语句
    Swift入门篇-基本类型(3)
    Swift入门篇-基本类型(2)
    Swift入门篇-基本类型(1)
  • 原文地址:https://www.cnblogs.com/xianz666/p/13206861.html
Copyright © 2011-2022 走看看