| | |
| | | |
| | | @Mapper |
| | | public interface OthFileMapper { |
| | | OthFile selectByPrimaryKey(Long id); |
| | | int deleteByPrimaryKey(Long id); |
| | | |
| | | int insert(OthFile record); |
| | | |
| | | int insertSelective(OthFile record); |
| | | |
| | | OthFile selectByPrimaryKey(Long id); |
| | | |
| | | int updateByPrimaryKeySelective(OthFile record); |
| | | |
| | | int updateByPrimaryKey(OthFile record); |
| | | |
| | | int deleteByPrimaryKey(Long id); |
| | | } |
| | |
| | | rvo = this.restCreateFileName(fmUrl, fileExtName) ; |
| | | if(rvo != null){ |
| | | String relativeFilePath = this.restSaveFile(fileCtrlRqMp, fileMethodMp, file, regionNum, json, rvo); |
| | | //log.info("存储文件生成文件路径:" + relativeFilePath); |
| | | if(relativeFilePath != null){ |
| | | rvo.createFilePath(relativeFilePath, rvo.fileNameHash); |
| | | } |
| | | rvo.fileWebPath = relativeFilePath ; |
| | | //if(relativeFilePath != null){ |
| | | // rvo.createFilePath(relativeFilePath, rvo.fileNameHash); |
| | | //} |
| | | } |
| | | } |
| | | return rvo ; |
| | |
| | | public String fileSysAbsolutePath; //文件名称的哈希值对应的文件最终存储绝对路径中的根目录,在配置文件中配置 |
| | | public String fileSysRelativePath; //文件名称的哈希值对应的文件最终存储相对路径的目录,在配置文件中配置 |
| | | public String fileSysRestUrl; //文件名称的哈希值对应的文件系统的restful URL,在配置文件中配置 |
| | | public String fileWebPath; //文件名称的哈希值对应的文件系统的下载文件的web path,动态生成 |
| | | public String fileWebUrl; //文件名称的哈希值对应的文件系统的下载文件的web URL,在配置文件中配置 |
| | | public String fileWebPath; //文件名称的哈希值对应的文件系统的显示及下载文件的web path,动态生成 |
| | | public String fileWebUrl; //文件名称的哈希值对应的文件系统的显示文件的web URL,在配置文件中配置 |
| | | public String fileWebDownloadUrl; //文件名称的哈希值对应的文件系统的Action下载文件的web URL,在配置文件中配置 |
| | | |
| | | public String toString(){ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 此方法不用了,因为文件名称后面加上 ?hashcode后,下载文件类中将找到不文件 |
| | | * 重新创建fileSysWebUrl |
| | | * @param relativeFilePath |
| | | * @param hashcode |
| | | */ |
| | | public void createFilePath(String relativeFilePath, Integer hashcode){ |
| | | if(relativeFilePath != null){ |
| | | if(relativeFilePath.indexOf("?") < 0){ |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | */ |
| | | } |
| | |
| | | public String extName ;//文件扩展名 |
| | | public String webPath ;//web文件访问路径 |
| | | public String webPathZip;//照片文件缩略图访问路径,其他类型文件此属性为null |
| | | public String downloadPath;//web文件下载路径,应用时路径后面加上 ?id=[id] |
| | | |
| | | public FileVo(){} |
| | | |
| | |
| | | String orgName, |
| | | String extName, |
| | | String webPath, |
| | | String webPathZip){ |
| | | String webPathZip, |
| | | String downloadPath){ |
| | | this.id = id ; |
| | | this.hash = hash ; |
| | | this.orgName = orgName ; |
| | | this.extName = extName ; |
| | | this.webPath = webPath ; |
| | | this.webPathZip = webPathZip ; |
| | | this.downloadPath = downloadPath ; |
| | | } |
| | | } |
| | |
| | | import com.dy.pmsGlobal.dyFile.FileRestVo; |
| | | import com.dy.pmsGlobal.dyFile.FileVo; |
| | | import com.dy.pmsGlobal.pojoOth.OthFile; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | |
| | | @PostMapping("/upPhoto") |
| | | @SsoPowerAop(power = "-1") //登录与权限同时验证 |
| | | //https://blog.zhengru.top/posts/33486.html#%E5%8D%95%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0 |
| | | public BaseResponse<?> upPhoto(MultipartFile file, HttpServletRequest req) { |
| | | public BaseResponse<?> upPhoto(MultipartFile file) { |
| | | try { |
| | | if (file != null) { |
| | | String[] fileNameGrp = fileOp.splitFileName(file) ; |
| | |
| | | fileNameGrp[1], |
| | | null); |
| | | String fileMainName = fileOp.getFileMainName(frVo.fileName) ; |
| | | Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath); |
| | | FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1], (frVo.getFileWebUrl() + frVo.getFileWebPath()), fileOp.getImgFileZipPath(frVo.getFileWebUrl() + frVo.getFileWebPath())) ; |
| | | Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath, frVo.fileWebDownloadUrl); |
| | | FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1], (frVo.getFileWebUrl() + frVo.getFileWebPath()), fileOp.getImgFileZipPath(frVo.getFileWebUrl() + frVo.getFileWebPath()), frVo.fileWebDownloadUrl) ; |
| | | return BaseResponseUtils.buildSuccess(fvo) ; |
| | | }else { |
| | | return BaseResponseUtils.buildError("未得到上传文件的扩展名"); |
| | |
| | | |
| | | @PostMapping("/upPhone") |
| | | @SsoPowerAop(power = "-1") |
| | | public BaseResponse<?> upPhone(MultipartFile file, HttpServletRequest req) { |
| | | public BaseResponse<?> upPhone(MultipartFile file) { |
| | | try { |
| | | if (file != null) { |
| | | String[] fileNameGrp = fileOp.splitFileName(file) ; |
| | |
| | | fileNameGrp[1], |
| | | null); |
| | | String fileMainName = fileOp.getFileMainName(frVo.fileName) ; |
| | | Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath); |
| | | FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1],frVo.getFileWebUrl() + frVo.getFileWebPath(), null) ; |
| | | Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath, frVo.fileWebDownloadUrl); |
| | | FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1],frVo.getFileWebUrl() + frVo.getFileWebPath(), null, frVo.fileWebDownloadUrl) ; |
| | | return BaseResponseUtils.buildSuccess(fvo) ; |
| | | }else { |
| | | return BaseResponseUtils.buildError("未得到上传文件的扩展名"); |
| | |
| | | |
| | | @PostMapping("/upVideo") |
| | | @SsoPowerAop(power = "-1") |
| | | public BaseResponse<?> upVideo(MultipartFile file, HttpServletRequest req) { |
| | | public BaseResponse<?> upVideo(MultipartFile file) { |
| | | try { |
| | | if (file != null) { |
| | | String[] fileNameGrp = fileOp.splitFileName(file) ; |
| | |
| | | fileNameGrp[1], |
| | | null); |
| | | String fileMainName = fileOp.getFileMainName(frVo.fileName) ; |
| | | Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath); |
| | | FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1],frVo.getFileWebUrl() + frVo.getFileWebPath(), null) ; |
| | | Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath, frVo.fileWebDownloadUrl); |
| | | FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1],frVo.getFileWebUrl() + frVo.getFileWebPath(), null, frVo.fileWebDownloadUrl) ; |
| | | return BaseResponseUtils.buildSuccess(fvo) ; |
| | | }else { |
| | | return BaseResponseUtils.buildError("未得到上传文件的扩展名"); |
| | |
| | | |
| | | @PostMapping("/upDocument") |
| | | @SsoPowerAop(power = "-1") |
| | | public BaseResponse<?> upDocument(MultipartFile file, HttpServletRequest req) { |
| | | public BaseResponse<?> upDocument(MultipartFile file) { |
| | | try { |
| | | if (file != null) { |
| | | String[] fileNameGrp = fileOp.splitFileName(file) ; |
| | |
| | | fileNameGrp[1], |
| | | null); |
| | | String fileMainName = fileOp.getFileMainName(frVo.fileName) ; |
| | | Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath); |
| | | FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1],frVo.getFileWebUrl() + frVo.getFileWebPath(), null) ; |
| | | Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath, frVo.fileWebDownloadUrl); |
| | | FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1],frVo.getFileWebUrl() + frVo.getFileWebPath(), null, frVo.fileWebDownloadUrl) ; |
| | | return BaseResponseUtils.buildSuccess(fvo) ; |
| | | }else { |
| | | return BaseResponseUtils.buildError("未得到上传文件的扩展名"); |
| | |
| | | * @param filePath 文件服务端存储相对路径 |
| | | * @return |
| | | */ |
| | | private Long saveFileInfo(String orgName, String extName, String newName, Integer hash, String filePath){ |
| | | private Long saveFileInfo(String orgName, String extName, String newName, Integer hash, String filePath, String downloadPath){ |
| | | OthFile po = new OthFile() ; |
| | | po.orgName = orgName ; |
| | | po.extName = extName ; |
| | | po.newName = newName ; |
| | | po.hash = hash ; |
| | | po.filePath = filePath ; |
| | | po.downloadPath = downloadPath ; |
| | | return this.sv.save(po) ; |
| | | } |
| | | |
| | |
| | | package com.dy.pmsGlobal.pojoOth; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.TableName; |
| | | import com.alibaba.fastjson2.annotation.JSONField; |
| | | import com.alibaba.fastjson2.writer.ObjectWriterImplToString; |
| | | import com.baomidou.mybatisplus.annotation.*; |
| | | import com.dy.common.po.BaseEntity; |
| | | import com.fasterxml.jackson.annotation.JsonFormat; |
| | | import lombok.*; |
| | | |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * 上载的文件信息 |
| | | */ |
| | | //2024-04-12下面TableName不用配置表名称(value="BaUser"或“ba_user”) |
| | | //只要通过驼峰命名法则类名与表名对应起来就可以了,如果不能对应起来,需要指定表名称 |
| | | //例如@TableName(value="TestUser" autoResultMap = true) |
| | | @TableName(value="ba_role", autoResultMap = true) |
| | | |
| | | @TableName(value="Oth_file", autoResultMap = true) |
| | | @Data |
| | | @Builder |
| | | @ToString |
| | | @NoArgsConstructor |
| | | @AllArgsConstructor |
| | | public class OthFile implements BaseEntity { |
| | | |
| | | public static final long serialVersionUID = 202404231631001L; |
| | | /** |
| | | * 主键 |
| | | * 主键 |
| | | */ |
| | | /* 如果不明确 type类型,MP将自动为其赋值(雪花ID) |
| | | IdType: |
| | | AUTO(0), //自增 |
| | | NONE(1), //未设置主键 |
| | | INPUT(2), //手动输入 |
| | | ASSIGN_ID(3), //默认全局唯一ID |
| | | ASSIGN_UUID(4), //全局唯一的 uuid |
| | | */ |
| | | @JSONField(serializeUsing= ObjectWriterImplToString.class) |
| | | @TableId(value = "id", type = IdType.INPUT) |
| | | public Long id; |
| | | |
| | | /** |
| | | * 文件原名称 |
| | | */ |
| | | * 文件原名称 |
| | | */ |
| | | public String orgName; |
| | | |
| | | /** |
| | | * 扩展名 |
| | | */ |
| | | * 扩展名 |
| | | */ |
| | | public String extName; |
| | | |
| | | /** |
| | | * 上传文件后系统自动给文件赋的新名称 |
| | | */ |
| | | * 上传文件后系统自动给文件赋的新名称 |
| | | */ |
| | | public String newName; |
| | | |
| | | /** |
| | | * 文件hash值 |
| | | */ |
| | | * 文件hash值 |
| | | */ |
| | | public Integer hash; |
| | | |
| | | /** |
| | | * 文件在服务端存储的相对路径 |
| | | */ |
| | | * 文件在服务端存储的相对路径 |
| | | */ |
| | | public String filePath; |
| | | |
| | | /** |
| | | * 文件下载路径 |
| | | */ |
| | | public String downloadPath; |
| | | |
| | | /** |
| | | * 文件上载日期 |
| | | */ |
| | | @TableField(value = "dt", fill = FieldFill.INSERT) |
| | | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
| | | public Date dt; |
| | | |
| | | } |
| | |
| | | hashEnd: 5461 |
| | | restUrl: http://127.0.0.1:${pms.file1.webPort}/file #file是上下文 |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ #nginx服务路径 |
| | | webDownloadUrl: http://127.0.0.1:${pms.file1.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file1.webPort}/file/download/down?id= |
| | | sv2: |
| | | id: dyFile2 |
| | | absolutePath: E:/java/nginx-1.24.0/html/webfiles/ |
| | |
| | | hashEnd: 10923 |
| | | restUrl: http://127.0.0.1:${pms.file2.webPort}/file |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ |
| | | webDownloadUrl: http://127.0.0.1:${pms.file2.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file2.webPort}/file/download/down?id= |
| | | sv3: |
| | | id: dyFile3 |
| | | absolutePath: E:/java/nginx-1.24.0/html/webfiles/ |
| | |
| | | hashEnd: 16385 |
| | | restUrl: http://127.0.0.1:${pms.file3.webPort}/file |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ |
| | | webDownloadUrl: http://127.0.0.1:${pms.file3.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file3.webPort}/file/download/down?id= |
| | | sv4: |
| | | id: dyFile4 |
| | | absolutePath: E:/java/nginx-1.24.0/html/webfiles/ |
| | |
| | | hashEnd: 21847 |
| | | restUrl: http://127.0.0.1:${pms.file4.webPort}/file |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ |
| | | webDownloadUrl: http://127.0.0.1:${pms.file4.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file4.webPort}/file/download/down?id= |
| | | sv5: |
| | | id: dyFile5 |
| | | absolutePath: E:/java/nginx-1.24.0/html/webfiles/ |
| | |
| | | hashEnd: 27309 |
| | | restUrl: http://127.0.0.1:${pms.file5.webPort}/file |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ |
| | | webDownloadUrl: http://127.0.0.1:${pms.file5.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file5.webPort}/file/download/down?id= |
| | | sv6: |
| | | id: dyFile6 |
| | | absolutePath: E:/java/nginx-1.24.0/html/webfiles/ |
| | |
| | | hashEnd: 32767 |
| | | restUrl: http://127.0.0.1:${pms.file6.webPort}/file |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ |
| | | webDownloadUrl: http://127.0.0.1:${pms.file6.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file6.webPort}/file/download/down?id= |
| | | sv7: |
| | | id: dyFile7 |
| | | absolutePath: E:/java/nginx-1.24.0/html/webfiles/ |
| | |
| | | hashEnd: 38229 |
| | | restUrl: http://127.0.0.1:${pms.file7.webPort}/file |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ |
| | | webDownloadUrl: http://127.0.0.1:${pms.file7.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file7.webPort}/file/download/down?id= |
| | | sv8: |
| | | id: dyFile8 |
| | | absolutePath: E:/java/nginx-1.24.0/html/webfiles/ |
| | |
| | | hashEnd: 43691 |
| | | restUrl: http://127.0.0.1:${pms.file8.webPort}/file |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ |
| | | webDownloadUrl: http://127.0.0.1:${pms.file8.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file8.webPort}/file/download/down?id= |
| | | sv9: |
| | | id: dyFile9 |
| | | absolutePath: E:/java/nginx-1.24.0/html/webfiles/ |
| | |
| | | hashEnd: 49153 |
| | | restUrl: http://127.0.0.1:${pms.file9.webPort}/file |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ |
| | | webDownloadUrl: http://127.0.0.1:${pms.file9.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file9.webPort}/file/download/down?id= |
| | | sv10: |
| | | id: dyFile10 |
| | | absolutePath: E:/java/nginx-1.24.0/html/webfiles/ |
| | |
| | | hashEnd: 54615 |
| | | restUrl: http://127.0.0.1:${pms.file10.webPort}/file |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ |
| | | webDownloadUrl: http://127.0.0.1:${pms.file10.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file10.webPort}/file/download/down?id= |
| | | sv11: |
| | | id: dyFile11 |
| | | absolutePath: E:/java/nginx-1.24.0/html/webfiles/ |
| | |
| | | hashEnd: 60077 |
| | | restUrl: http://127.0.0.1:${pms.file11.webPort}/file |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ |
| | | webDownloadUrl: http://127.0.0.1:${pms.file11.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file11.webPort}/file/download/down?id= |
| | | sv12: |
| | | id: dyFile12 |
| | | absolutePath: E:/java/nginx-1.24.0/html/webfiles/ |
| | |
| | | hashEnd: 65535 |
| | | restUrl: http://127.0.0.1:${pms.file12.webPort}/file |
| | | webUrl: http://127.0.0.1:${pms.nginx.webPort}/webfiles/ |
| | | webDownloadUrl: http://127.0.0.1:${pms.file12.webPort}/file/download/down |
| | | webDownloadUrl: http://127.0.0.1:${pms.file12.webPort}/file/download/down?id= |
| | |
| | | <result column="new_name" property="newName" /> |
| | | <result column="hash" property="hash" /> |
| | | <result column="file_path" property="filePath" /> |
| | | <result column="download_path" property="downloadPath" /> |
| | | <result column="dt" property="dt" /> |
| | | </resultMap> |
| | | <sql id="Base_Column_List"> |
| | | <!--@mbg.generated--> |
| | | id, org_name, ext_name, new_name, hash, file_path |
| | | id, org_name, ext_name, new_name, hash, file_path, download_path, dt |
| | | </sql> |
| | | <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> |
| | | <!--@mbg.generated--> |
| | |
| | | </delete> |
| | | <insert id="insert" parameterType="com.dy.pmsGlobal.pojoOth.OthFile"> |
| | | <!--@mbg.generated--> |
| | | insert into oth_file (id, org_name, ext_name, new_name, hash, file_path) |
| | | values (#{id}, #{orgName}, #{extName}, #{newName}, #{hash}, #{filePath}) |
| | | insert into oth_file (id, org_name, ext_name, new_name, hash, file_path, download_path, |
| | | dt) |
| | | values (#{id}, #{orgName}, #{extName}, #{newName}, #{hash}, #{filePath}, #{downloadPath}, |
| | | #{dt}) |
| | | </insert> |
| | | <insert id="insertSelective" parameterType="com.dy.pmsGlobal.pojoOth.OthFile"> |
| | | <!--@mbg.generated--> |
| | |
| | | <if test="filePath != null"> |
| | | file_path, |
| | | </if> |
| | | <if test="downloadPath != null"> |
| | | download_path, |
| | | </if> |
| | | <if test="dt != null"> |
| | | dt, |
| | | </if> |
| | | </trim> |
| | | <trim prefix="values (" suffix=")" suffixOverrides=","> |
| | | <if test="id != null"> |
| | |
| | | </if> |
| | | <if test="filePath != null"> |
| | | #{filePath}, |
| | | </if> |
| | | <if test="downloadPath != null"> |
| | | #{downloadPath}, |
| | | </if> |
| | | <if test="dt != null"> |
| | | #{dt}, |
| | | </if> |
| | | </trim> |
| | | </insert> |
| | |
| | | <if test="filePath != null"> |
| | | file_path = #{filePath}, |
| | | </if> |
| | | <if test="downloadPath != null"> |
| | | download_path = #{downloadPath}, |
| | | </if> |
| | | <if test="dt != null"> |
| | | dt = #{dt}, |
| | | </if> |
| | | </set> |
| | | where id = #{id} |
| | | </update> |
| | |
| | | ext_name = #{extName}, |
| | | new_name = #{newName}, |
| | | hash = #{hash}, |
| | | file_path = #{filePath} |
| | | file_path = #{filePath}, |
| | | download_path = #{downloadPath}, |
| | | dt = #{dt} |
| | | where id = #{id} |
| | | </update> |
| | | </mapper> |
New file |
| | |
| | | package com.dy.dyFile.download; |
| | | |
| | | import cn.hutool.http.server.HttpServerResponse; |
| | | import com.dy.pmsGlobal.dyFile.FileOperate; |
| | | import com.dy.pmsGlobal.dyFile.FileRestVo; |
| | | import com.dy.pmsGlobal.pojoOth.OthFile; |
| | | import jakarta.servlet.ServletOutputStream; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | |
| | | /** |
| | | * web文件下载 |
| | | */ |
| | | @Slf4j |
| | | @RestController |
| | | @RequestMapping(path="download") |
| | | @SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked") |
| | | public class DownloadFileCtrl { |
| | | |
| | | @Autowired |
| | | private DownloadFileSv sv ; |
| | | |
| | | @Autowired |
| | | private FileOperate fileOp ; |
| | | |
| | | @Value("${dy.webFile.fmUrl}") |
| | | private String fmUrl ; |
| | | |
| | | |
| | | /** |
| | | * 通过web文件数据库实体id下载该文件 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @GetMapping("/down") |
| | | public void down(String id, HttpServletRequest req, HttpServletResponse rep){ |
| | | OthFile fPo = sv.selectById(id) ; |
| | | if(fPo != null){ |
| | | FileRestVo frVo = fileOp.parseHashcode(fmUrl, fPo.hash) ; |
| | | if(frVo.fileSysAbsolutePath != null){ |
| | | if(!frVo.fileSysAbsolutePath.endsWith("\\\\") && !frVo.fileSysAbsolutePath.endsWith("/") ){ |
| | | frVo.fileSysAbsolutePath = frVo.fileSysAbsolutePath + "/" ; |
| | | } |
| | | } |
| | | String filePath = frVo.fileSysAbsolutePath + fPo.filePath ; |
| | | File f = new File(filePath) ; |
| | | if(f.exists()){ |
| | | String fileReName = fPo.orgName + "." + fPo.extName ; |
| | | try { |
| | | fileReName = new String(fileReName.getBytes("UTF-8"), "ISO-8859-1"); |
| | | } catch (Exception e) { |
| | | fileReName = "file" ; |
| | | } |
| | | rep.addHeader("content-type", "application/octet-stream"); |
| | | rep.addHeader("Content-Disposition", "attachment;fileName=" + fileReName); |
| | | |
| | | ServletOutputStream out = null; |
| | | FileInputStream in = null ; |
| | | try { |
| | | out = rep.getOutputStream() ; |
| | | } catch (Exception ee) { |
| | | out = null ; |
| | | }finally{ |
| | | if(out != null){ |
| | | byte[] bs = new byte[1024] ; |
| | | int len = -1 ; |
| | | try { |
| | | in = new FileInputStream(f); |
| | | len = in.read(bs) ; |
| | | while(len != -1){ |
| | | out.write(bs, 0, len); |
| | | len = in.read(bs) ; |
| | | } |
| | | } catch (Exception eee) { |
| | | } finally { |
| | | if(out != null){ |
| | | try{ |
| | | out.flush(); |
| | | out.close(); |
| | | }catch(Exception e){ |
| | | }finally{ |
| | | if(in != null){ |
| | | try{ |
| | | in.close(); |
| | | }catch(Exception e){ |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }else{ |
| | | } |
| | | }else{ |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.dyFile.download; |
| | | |
| | | |
| | | import com.dy.common.util.NumUtil; |
| | | import com.dy.pmsGlobal.daoBa.BaUserMapper; |
| | | import com.dy.pmsGlobal.daoOth.OthFileMapper; |
| | | import com.dy.pmsGlobal.pojoOth.OthFile; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | @Slf4j |
| | | @Service |
| | | public class DownloadFileSv { |
| | | @Autowired |
| | | private OthFileMapper dao; |
| | | |
| | | /** |
| | | * 用ID查询 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | public OthFile selectById(String id){ |
| | | if(NumUtil.isPlusIntNumber(id)){ |
| | | return dao.selectByPrimaryKey(Long.parseLong(id)) ; |
| | | }else{ |
| | | return null ; |
| | | } |
| | | } |
| | | |
| | | } |
| | |
| | | import java.io.InputStream; |
| | | |
| | | /** |
| | | * web文件上传 |
| | | * web文件上传, 内部调用,即由其他子模块调用, |
| | | * 一般不由前端系统调用 |
| | | */ |
| | | @Slf4j |
| | | @RestController |
| | |
| | | } |
| | | if(this.webDownloadUrl == null || this.webDownloadUrl.trim().equals("")){ |
| | | throw new Exception("出错,webDownloadUrl为空了!") ; |
| | | }else{ |
| | | this.webDownloadUrl = this.webDownloadUrl.trim() ; |
| | | if(!this.webDownloadUrl.endsWith("/") && !this.webDownloadUrl.endsWith("\\")){ |
| | | this.webDownloadUrl += "/" ; |
| | | } |
| | | } |
| | | |
| | | if(this.hashStart == null){ |
| | | throw new Exception("出错,hashStart为空了!") ; |
| | | }else if(this.hashStart.intValue() < 0){ |