Java实现带表单参数的文件上传、下载和文件压缩打包下载

准备工作

知识准备

  1. SpringBoot
  2. Maven
  3. Spring Data JPA

工具准备
IDE:IDEA

说明

在此项目中, 我将文件上传的一些参数(例如上传路径、下载路径、文件名等)保存在数据库当中,这样更加灵活。当项目部署后,只需要更改数据库中的数据就可以实现不同的操作,不用再重新部署,但我也将不使用数据库的写法保留在代码中,以供参考

项目结构

在这里插入图片描述

实体类

FileConfiguration.java

import lombok.Data;

import javax.persistence.*;

/**
 * Created by FantasticPan on 2018/10/23.
 * 文件配置类,配置上传路径、下载路径、文件名等参数
 */
@Data
@Entity
@Table
public class FileConfiguration {
   

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    
    //指定下载的文件名字
    //eg:file.docx
    private String downloadFileName; 
    
    //指定生成的zip压缩包名字
    //eg:file.zip
    private String downloadZipFileName; 
    
    //允许文件上传类型
    //eg:.txt,.docx,.doc 注意:逗号为英文符
    private String uploadFileType; 
    
    //提示信息
    //eg:文件内容为空 !,文件大小限制1M !,文件后缀名有误 !,提交成功!,提交失败,请与工作人员联系 注意:逗号为英文符
    private String tips; 
    
    //指定文件上传的位置
    //eg:windows系统:D:/uploadFies  Linux系统:/file/uploadFiles
    private String uploadFilePath; 
    
    //指定要下载文件的所在路径
    //eg:windows系统:D:/file  Linux系统:/file
    private String downLoadFilePath; 
}

实体类使用Lombok工具简化GET、SET方法,不知道的可以直接写Get、Set方法
特别注意:允许文件上传类型uploadFileType和提示信息tips在数据库中逗号分隔符要用英文符

Files.java

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;

/**
 * Created by FantasticPan on 2017/12/5.
 * 文件类,保存上传文件的一些参数(上传日期、上传路径等)
 */
@Entity
@Table(name = "files")
@Getter
@Setter
@ToString
public class Files implements Serializable {
   

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;
    private String url;
    private Timestamp date;
}

文件上传,数据库一般是不保存文件的,只是将文件的位置等信息保存在数据库中,根据这些信息再到系统中去查找文件

Member.java

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

/**
 * Created by FantasticPan on 2018/1/24.
 * 对象类
 */
@Entity
@Table(name = "member")
@Setter
@Getter
public class Member {
   

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    String username;
    String password;

    public Member() {
   }

    public Member(String username,String password) {
   
        this.username = username;
        this.password = password;
    }
}

这是对象类,带表单参数上传文件时使用

DAO层

针对上面的三个实体类创建三个DAO接口,操作数据库,这里我使用的持久层数据库为Jpa

1.

import com.pan.file.entity.Files;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * Created by FantasticPan on 2017/12/5.
 */
public interface FileRepository extends JpaRepository<Files,Long> {
   
}

2.

import com.pan.file.entity.FileConfiguration;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

/**
 * Created by FantasticPan on 2018/10/23.
 */
public interface ConfigurationRepository extends JpaRepository<FileConfiguration, Long> {
   

    @Query("select f.downloadFileName from FileConfiguration f where f.id =?1")
    String findDownloadFileName(Integer id);

    @Query("select f.downloadZipFileName from FileConfiguration f where f.id =?1")
    String findDownloadZipFileName(Integer id);

    @Query("select f.uploadFileType from FileConfiguration f where f.id =?1")
    String findUploadFileType(Integer id);

    @Query("select f.tips from FileConfiguration f where f.id =?1")
    String findTips(Integer id);

    @Query("select f.uploadFilePath from FileConfiguration f where f.id =?1")
    String findUploadFilePath(Integer id);

    @Query("select f.downLoadFilePath from FileConfiguration f where f.id =?1")
    String findDownLoadFilePath(Integer id);
}

3.

import com.pan.file.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * Created by FantasticPan on 2018/1/24.
 */
public interface MemberRepository extends JpaRepository<Member, Long> {
   
}

1、文件上传

我将上传方法进行封装,方便调用:

import org.springframework.web.multipart.MultipartFile;
import java.io.File;

/**
  * 上传文件
  */
public static void uploadFile(String filePath, String fileName, MultipartFile multipartFile) throws Exception {
   
        File file = new File(filePath + fileName);
        if (!file.getParentFile().exists()) {
   
            file.getParentFile().mkdirs();
        }
        multipartFile.transferTo(file);
    }

第一个参数是要上传的位置,如果是本地可以是某一个盘下的文件夹路径,第二个参数是获取到的文件名字,第三个参数是我们使用 Multipartfile 来实现文件上传。
从数据库中查询默认查询主键id为1

上传方法调用

    @Autowired
    private FileRepository fileRepository;
    @Autowired
    private ConfigurationRepository configurationRepository;

    @PostMapping(value = "/submit")
    @ResponseBody
    public ModelAndView uploadFile(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "file") MultipartFile multipartFile) throws UnsupportedEncodingException {
   

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        Long length = multipartFile.getSize();//返回的是字节,1M=1024KB=1048576字节 1KB=1024Byte
        String fileName = multipartFile.getOriginalFilename();
        //获取文件后缀名
        String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase().trim();
        //获取
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值