Java Web一行代码实现文件上传下载

转自:https://blog.csdn.net/u013314786/article/details/49720671

每当要实现文件上传下载的功能时,都要复制粘贴拼凑代码。如果用了不同的框架,代码还不一样,配置啥的一堆,甚是繁琐,不喜欢。科学家们喜欢把纷繁复杂的自然现象总结为一个个简洁的公式,我们也来试试,把上传下载整成一行代码~

       花了一天时间,整了个通用的工具类FileUtils,这个类里实际只包含两个静态方法,一个上传upload(),一个下载download()。只依赖apache的commons-fileupload.jar和commons-io.jar,不依赖struts和spring mvc等框架。干净简洁通用。上传下载文件只需一行代码即可搞定。现将代码贴上来,与大家共享,谁有好的想法,欢迎提议。

工具类:FileUtils.java

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.UnsupportedEncodingException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.UUID;
     
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileUploadException;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
     
    public class FileUtils {
     
        private FileUtils() {
     
        }
     
        public static void download(HttpServletRequest request,
                HttpServletResponse response, String relativeFilePath) {
     
            try {
                request.setCharacterEncoding("UTF-8");
                response.setCharacterEncoding("UTF-8");
                
                String fileName = request.getSession().getServletContext()
                        .getRealPath("/")
                        + relativeFilePath;
                fileName = fileName.replace("\\", "/");//统一分隔符格式
                File file = new File(fileName);
                //如果文件不存在
                if (file == null || !file.exists()) {
                    String msg = "file not exists!";
                    System.out.println(msg);
                    PrintWriter out = response.getWriter();
                    out.write(msg);
                    out.flush();
                    out.close();
                    return;
                }
     
                String fileType = request.getSession().getServletContext()
                        .getMimeType(fileName);
                if (fileType == null) {
                    fileType = "application/octet-stream";
                }
                response.setContentType(fileType);
                System.out.println("文件类型是:" + fileType);
                String simpleName = fileName.substring(fileName.lastIndexOf("/")+1);
                String newFileName = new String(simpleName.getBytes(), "ISO8859-1");
                response.setHeader("Content-disposition", "attachment;filename="+newFileName);
     
                BufferedInputStream bis = new BufferedInputStream(
                        new FileInputStream(file));
                BufferedOutputStream bos = new BufferedOutputStream(
                        response.getOutputStream());
     
                byte[] buffer = new byte[1024];
                int length = 0;
     
                while ((length = bis.read(buffer)) != -1) {
                    bos.write(buffer, 0, length);
                }
     
                if (bis != null)
                    bis.close();
                if (bos != null)
                    bos.close();
     
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     
        /**
         * 文件上传
         *
         * @param request HttpServletRequest
         * @param relativeUploadPath 上传文件保存的相对路径,例如"upload/",注意,末尾的"/"不要丢了
         * @param maxSize 上传的最大文件尺寸,单位字节
         * @param thresholdSize 最大缓存,单位字节
         * @param fileTypes 文件类型,会根据上传文件的后缀名判断。<br>
         * 比如支持上传jpg,jpeg,gif,png图片,那么此处写成".jpg .jpeg .gif .png",<br>
         * 也可以写成".jpg/.jpeg/.gif/.png",类型之间的分隔符是什么都可以,甚至可以不要,<br>
         * 直接写成".jpg.jpeg.gif.png",但是类型前边的"."不能丢
         * @return
         */
        public static List<String> upload(HttpServletRequest request, String relativeUploadPath, int maxSize, int thresholdSize, String fileTypes) {
            // 设置字符编码
            try {
                request.setCharacterEncoding("UTF-8");
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }
     
            String tempPath = relativeUploadPath + "temp"; // 临时文件目录
            String serverPath = request.getSession().getServletContext().getRealPath("/").replace("\\", "/");
            fileTypes = fileTypes.toLowerCase(); // 将后缀全转换为小写
     
            //如果上传文件目录和临时目录不存在则自动创建
            if (!new File(serverPath + relativeUploadPath).isDirectory()) {
                new File(serverPath + relativeUploadPath).mkdirs();
            }
            if (!new File(serverPath + tempPath).isDirectory()) {
                new File(serverPath + tempPath).mkdirs();
            }
     
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setSizeThreshold(thresholdSize); // 最大缓存
            factory.setRepository(new File(serverPath + tempPath));// 临时文件目录
     
            ServletFileUpload upload = new ServletFileUpload(factory);
     
            upload.setSizeMax(maxSize);// 文件最大上限
     
            List<String> filePaths = new ArrayList<String>();
     
            List<FileItem> items;
            try {
                items = upload.parseRequest(request);
                // 获取所有文件列表
                for (FileItem item : items) {
                    
                    // 获得文件名,文件名包括路径
                    if (!item.isFormField()) { // 如果是文件
                        // 文件名
                        String fileName = item.getName().replace("\\", "/");
                        //文件后缀名
                        String suffix = null;
                        if (fileName.lastIndexOf(".") > -1) {
                            suffix = fileName.substring(fileName.lastIndexOf("."));
                        } else { //如果文件没有后缀名,不处理,直接跳过本次循环
                            continue;
                        }
                        
                        // 不包含路径的文件名
                        String SimpleFileName = fileName;
                        if (fileName.indexOf("/") > -1) {
                            SimpleFileName = fileName.substring(fileName
                                    .lastIndexOf("/") + 1);
                        }
     
                        // 如果文件类型字符串中包含该后缀名,保存该文件
                        if (fileTypes.indexOf(suffix.toLowerCase()) > -1) {
                            String uuid = UUID.randomUUID().toString();
                            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                            String absoluteFilePath = serverPath
                                    + relativeUploadPath + sf.format(new Date())
                                    + " " + uuid + " " + SimpleFileName;
                            item.write(new File(absoluteFilePath));
                            filePaths.add(absoluteFilePath);
                        }
                    }
                }
            } catch (FileUploadException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
     
            return filePaths;
        }
     
        /**
         * 文件上传
         *
         * @param request HttpServletRequest
         * @param relativeUploadPath 上传文件保存的相对路径,例如"upload/",注意,末尾的"/"不要丢了
         * @param maxSize 上传的最大文件尺寸,单位字节
         * @param fileTypes 文件类型,会根据上传文件的后缀名判断。<br>
         * 比如支持上传jpg,jpeg,gif,png图片,那么此处写成".jpg .jpeg .gif .png",<br>
         * 也可以写成".jpg/.jpeg/.gif/.png",类型之间的分隔符是什么都可以,甚至可以不要,<br>
         * 直接写成".jpg.jpeg.gif.png",但是类型前边的"."不能丢
         * @return
         */
        public static List<String> upload(HttpServletRequest request, String relativeUploadPath, int maxSize, String fileTypes) {
            return upload(request, relativeUploadPath, maxSize, 5*1024, fileTypes);
        }
        
        /**
         * 文件上传,不限大小
         *
         * @param request HttpServletRequest
         * @param relativeUploadPath 上传文件保存的相对路径,例如"upload/",注意,末尾的"/"不要丢了
         * @param fileTypes 文件类型,会根据上传文件的后缀名判断。<br>
         * 比如支持上传jpg,jpeg,gif,png图片,那么此处写成".jpg .jpeg .gif .png",<br>
         * 也可以写成".jpg/.jpeg/.gif/.png",类型之间的分隔符是什么都可以,甚至可以不要,<br>
         * 直接写成".jpg.jpeg.gif.png",但是类型前边的"."不能丢
         * @return
         */
        public static List<String> upload(HttpServletRequest request, String relativeUploadPath, String fileTypes) {
            return upload(request, relativeUploadPath, -1, 5*1024, fileTypes);
        }
    }

上传文件的方法又写了两个重载,用法一看便知,不再解释。

使用示例:FileUtilsTest.java

    import java.io.IOException;
    import java.util.List;
     
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import common.utils.FileUtils;
     
    public class FileUtilsTest extends HttpServlet {
     
        private static final long serialVersionUID = 1L;
     
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //下载文件
            FileUtils.download(request, response, "files/学生信息.xls");
        }
     
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //上传文件
            List<String> filePaths = FileUtils.upload(request, "upload/",".xls .xlsx");
            System.out.println(filePaths);
        }
    }


不管是用struts还是spring mvc还是原生的servlet,拿到request,response都是轻而易举吧,然后上传下载就只需要上边的一行代码了^_^

测试一下:

看看后台打印结果:

上传成功,再来测测下载:

也没问题。

以后再需要上传下载功能的时候,只需要导入两个jar包,把FileUtils类复制过去,然后FileUtils.download(),FileUtils.upload()就可以啦^_^

项目下载地址:http://download.csdn.net/detail/u013314786/9252777

考虑到项目版本问题,压缩包中只包含了src和WebRoot两个文件夹,要运行项目,只需在eclipse或者myeclipse中新建一个名为FileUtils的项目,然后把src里的文件复制到项目的src文件夹下,WebRoot里的文件复制到项目的WebRoot(myeclipse默认)或者WebContent(eclipse默认)文件夹下就可以运行啦。
---------------------
作者:浅醉樱花雨
来源:CSDN
原文:https://blog.csdn.net/u013314786/article/details/49720671
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Spring Boot框架的Java代码示例,演示了如何将上传的Excel文件读取并将其内容保存到数据库中: 1. 首先,在pom.xml文件中添加以下依赖项: ``` <!-- Apache POI --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> ``` 2. 创建一个名为UploadController的控制器类。该类包含一个处理文件上传请求的方法。 ```java @RestController public class UploadController { @Autowired private JdbcTemplate jdbcTemplate; @PostMapping("/upload") public String handleFileUpload(@RequestParam("file") MultipartFile file) throws IOException { // 读取Excel文件 Workbook workbook = new XSSFWorkbook(file.getInputStream()); Sheet sheet = workbook.getSheetAt(0); // 遍历行 for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); // 读取单元格的值 String name = row.getCell(0).getStringCellValue(); String email = row.getCell(1).getStringCellValue(); // 将数据保存到数据库中 jdbcTemplate.update("INSERT INTO users (name, email) VALUES (?, ?)", name, email); } return "File uploaded successfully!"; } } ``` 3. 创建一个名为application.properties的文件,配置数据库连接信息。 ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=myusername spring.datasource.password=mypassword spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 4. 运行Spring Boot应用程序,并在浏览器中访问上传文件的URL。在选择一个Excel文件并上传后,文件内容将被读取并保存到MySQL数据库中。 注意:该示例假设已经按照正确的方式设置了数据库连接信息,并且已经创建了一个名为"users"的表,该表包含"name"和"email"两个列。如果您的数据库结构与此不同,请相应地修改代码

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值