spring boot项目实现上传与下载——(java上传篇)

1、最近在使用spring boot框架做一个项目,这里面包含着众多的上传于下载工作,在这里我做了一些总结和归纳。

2、spring boot项目的默认Tomcat规定上传文件的最大值不得超过1M,如果超过了 ,在spring boot的控制台就会抛错,所以咱们需要首先在spring boot项目的配置文件application.properties文件中添加配置。

#配置文件上传的限制
spring.servlet.multipart.enabled=true
#单个文件大小限制
spring.servlet.multipart.max-file-size=10MB
#设置总上传数据大小
spring.servlet.multipart.max-request-size=1000MB

3、文件上传这边使用的是传统form表单进行上传。后台进行接收上传的数据。

  @Override
    public int addFile(File2 ffile, HttpServletRequest request, MultipartFile fileField) throws Exception {


        String fileName = fileField.getOriginalFilename();
        String fileUrl=null;
        BufferedOutputStream bos = null;
        try {
            File path=new File(ResourceUtils.getURL("classpath:").getPath());
            if(!path.exists()) path = new File("");
            //构建真实的文件路径
            File newFile = new File(path.getAbsolutePath(),"static/assets/file/");
            if(!newFile.exists()) newFile.mkdirs();
            //转存文件到指定路径,如果文件名重复的话,将会覆盖掉之前的文件,这里是把文件上传到 “绝对路径”
            // Check for Unix-style path
            int unixSep = fileName.lastIndexOf('/');
            // Check for Windows-style path
            int winSep = fileName.lastIndexOf('\\');
            // Cut off at latest possible point
            int pos = (winSep > unixSep ? winSep : unixSep);
            if (pos != -1)  {
                // Any sort of path separator found...
                fileName = fileName.substring(pos + 1);
            }

            String backgroundPicUrl = newFile.getAbsolutePath()+ "\\" + ffile.getFileName()
                    + fileName.substring(fileName.lastIndexOf('.'));

            byte[] bytes = fileField.getBytes();
            bos = new BufferedOutputStream(new FileOutputStream(new File(backgroundPicUrl)));
            bos.write(bytes);


            fileUrl=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+"/assets/file/" +ffile.getFileName() + fileName.substring(fileName.lastIndexOf('.'));

        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info( user.getUserName()+"用户添加了名字为"+ffile.getFileName()+"的文件" );
}

但上传的时候有些浏览器上传的时候:String fileName = fileField.getOriginalFilename();获取到的值不同,谷歌浏览器是获取的文件名,而ie浏览器,edge浏览器中fileName获取到时候是上传文件的地址,所以为了解决这一问题,可以直接取出上传文件的类型,然后重新定义一下文件名进行保存。

  int unixSep = fileName.lastIndexOf('/');
            // Check for Windows-style path
            int winSep = fileName.lastIndexOf('\\');
            // Cut off at latest possible point
            int pos = (winSep > unixSep ? winSep : unixSep);
            if (pos != -1)  {
                // Any sort of path separator found...
                fileName = fileName.substring(pos + 1);
            }

最终将上传文件的路径url保存在数据库中。

4、这边有一个上传文件存储位置的问题,一些客户要求存储位置不能为服务器的绝对路径,出现C盘,D盘等问题,所以在这开发者基本都会将文件保存在本项目下的相对路径中。在spring boot项目中在本地的时候 文件会保存在target文件下面,在服务器中就会存储在相对路径下面。

 File path=new File(ResourceUtils.getURL("classpath:").getPath());
           
            //构建真实的文件路径
            File newFile = new File(path.getAbsolutePath(),"static/assets/file/");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在个人博客系统中,拓展模块是非常重要的,因为它可以提供给用户更多的功能和服务。在本文中,我们将介绍如何在 Spring Boot 项目实现一个拓展模块。 首先,我们需要先定义一个接口,用于规范拓展模块的实现: ```java public interface BlogModule { String moduleName(); // 获取模块名称 void start(); // 模块启动方法 void stop(); // 模块停止方法 List<String> addPage(String pageName); // 增加页面 } ``` 接下来,我们需要创建一个基础的拓展模块,作为其他拓展模块的基类: ```java public abstract class BaseBlogModule implements BlogModule { private String moduleName; public BaseBlogModule(String moduleName) { this.moduleName = moduleName; } @Override public String moduleName() { return moduleName; } } ``` 在基类中,我们实现了 `moduleName()` 方法,返回拓展模块的名称,并在构造函数中初始化了名称。 然后,我们可以开始实现具体的拓展模块了。假设我们需要实现一个功能,可以在博客系统中添加页面。我们可以创建一个名为 `AddPageModule` 的拓展模块: ```java public class AddPageModule extends BaseBlogModule { private List<String> pages = new ArrayList<>(); public AddPageModule() { super("AddPageModule"); } @Override public void start() { System.out.println("AddPageModule starting..."); } @Override public void stop() { System.out.println("AddPageModule stopping..."); } @Override public List<String> addPage(String pageName) { pages.add(pageName); return pages; } } ``` 在这个拓展模块中,我们实现了 `start()` 和 `stop()` 方法,用于在模块启动和停止时执行一些操作。同时,我们还实现了 `addPage()` 方法,用于添加页面,并返回当前系统中所有的页面。 最后,我们需要在 Spring Boot 项目中将这个拓展模块注入到容器中,并在需要的地方调用它的方法。我们可以使用 `@Autowired` 注解将拓展模块注入到其他组件中: ```java @Service public class BlogService { @Autowired private List<BlogModule> modules; public List<String> addPage(String pageName) { List<String> pages = new ArrayList<>(); for (BlogModule module : modules) { pages.addAll(module.addPage(pageName)); } return pages; } } ``` 在这个示例中,我们使用 `@Autowired` 注解将 `BlogModule` 的实现类自动注入到 `modules` 列表中,并在 `addPage()` 方法中循环遍历所有的模块,调用它们的 `addPage()` 方法。 通过这种方式,我们可以轻松地实现一个可拓展的 Spring Boot 项目,并在需要的时候动态地增加或删除拓展模块。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值