13、文件上传和下载、UUID、注解案例

文件的上传和下载

文件上传

在JavaWeb阶段的文件上传我们需要借助Apache下的commons-iocommons-fileupload两个jar包。

上传文件的前台页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>上传文件</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
    <%--action使用EL表达式获取项目上下文路径,method使用post请求--%>
    用户名:<input type="text" name="username">
    <br>
    <input type="file" name="file">
    <br>
    <input type="submit" value="提交">
</form>
</body>
</html>

使用Wireshark抓取请求/upload时的POST请求体数据。

在这里插入图片描述

后台在收到前台提交的POST请求后:

package org.westos.demo;

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;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author lwj
 * @date 2020/8/25 9:16
 */
@WebServlet(name = "UploadServlet", value = "/upload")
public class UploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("utf-8");

            String realPath = getServletContext().getRealPath("/upload");
            File uploadDir = new File(realPath);
            if (!uploadDir.exists()) {
                uploadDir.mkdir();
            }

            //我们使用Apache提供的commons-io和commons-fileupload两个jar包
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
            List<FileItem> fileItems = servletFileUpload.parseRequest(request);
            for (FileItem item : fileItems) {
                if (item.isFormField()) {
                    //判断是否是普通表单项
                    String fieldName = item.getFieldName();
                    System.out.println(fieldName);
                    String fieldValue = item.getString("utf-8");
                    System.out.println(fieldValue);
                } else {
                    //这个分支是文件表单项
                    String fieldName = item.getFieldName();
                    System.out.println(fieldName);
                    String name = item.getName();
                    //文件名
                    long size = item.getSize();
                    System.out.println("上传的" + name + "文件大小为:" + size + "B");
                    String contentType = item.getContentType();
                    System.out.println("文件类型为:" + contentType);
                    //将文件保存到服务器
                    File file = new File(uploadDir, UUID.randomUUID() + "-" + name);
                    //当文件名相同时容易造成覆盖,生成唯一的方法
                    //1、System.currentTimeMillis()
                    //2、UUID
                    item.write(file);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

在这里插入图片描述

UUID

System.currentTimeMillis()

long类型的时间戳

UUID

String string = UUID.randomUUID().toString();
System.out.println(string);

bb384e27-22a5-4191-b1ec-9b23789064bc

be871b39-0a1f-4bdc-8475-23ce01a9d8a7

3575b2a7-4a56-449c-9029-8f65ac17534c

文件下载

前台页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件下载</title>
</head>
<body>
    <a href="${pageContext.request.contextPath}/download?filename=3f05.jpeg">图片下载</a>
    <hr>
    <a href="${pageContext.request.contextPath}/download?filename=乌兹.jpeg">乌兹图片下载</a>
</body>
</html>

后台处理逻辑

package org.westos.demo;

import org.apache.commons.io.IOUtils;
import sun.misc.BASE64Encoder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author lwj
 * @date 2020/8/25 10:33
 */
@WebServlet(name = "DownServlet", value = "/download")
public class DownServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String realPath = getServletContext().getRealPath("/img");
        String filename = request.getParameter("filename");
        System.out.println(filename);
        //3f05.jpeg
        //乌兹.jpeg
        FileInputStream fis = new FileInputStream(new File(realPath, filename));
        ServletOutputStream outputStream = response.getOutputStream();

        //如果图片中包含中文,那么将图片名称传递给浏览器之前需要编码图片名称
        String agent = request.getHeader("user-agent");
        if (agent.contains("MSIE")) {
            // IE浏览器
            filename = URLEncoder.encode(filename, "utf-8");
            filename = filename.replace("+", " ");
        } else if (agent.contains("Firefox")) {
            // 火狐浏览器
            BASE64Encoder base64Encoder = new BASE64Encoder();
            filename = "=?utf-8?B?"
                    + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
        } else if (agent.contains("Chrome")) {
            // google浏览器
            filename = URLEncoder.encode(filename, "utf-8");
        } else {
            // 其它浏览器
            filename = URLEncoder.encode(filename, "utf-8");
        }

        response.setHeader("content-disposition", "attachment;filename="
                + filename);
        //设置文件的类型
        response.setHeader("content-type", this.getServletContext()
                .getMimeType(filename));
        //设置响应头,告诉浏览器不要默认打开,而是弹出一个下载框
        IOUtils.copy(fis, outputStream);
        //通过commons-io包下的工具类进行流的拷贝
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

在这里插入图片描述

在请求后台的图片时,浏览器默认会将请求URL中的中文进行URL编码。

URL编码

package org.westos.demo;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class MyTest4 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "乌兹";
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        System.out.println(bytes.length);
        //6 utf-8编码,每个中文3个字节
        String[] strs = new String[bytes.length];
        int index = 0;
        for (byte b : bytes) {
            System.out.println(Integer.toBinaryString(b & 0xff));
            //11100100
            //10111001
            //10001100
            //11100101
            //10000101
            //10111001
            //高24位置0,低8位保持不变
            strs[index++] = Integer.toHexString(b & 0xff);
        }
        System.out.println(Arrays.toString(strs));
        //[e4, b9, 8c, e5, 85, b9]

        System.out.println(URLEncoder.encode("乌兹", "utf-8"));
        //%E4%B9%8C%E5%85%B9


        System.out.println(Integer.toBinaryString(0xe4));
        //11100100
        System.out.println(Integer.toBinaryString(-127));
        //11111111111111111111111110000001
    }
}

注解代替jdbc.properties

在前面学习JDBC获取连接时都是读取.properties配置文件来获取value。

现在使用注解来实现。

package org.westos.demo;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author lwj
 * @date 2020/8/25 13:56
 */
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD})
public @interface JDBCAnno {
    String driverClass() default "com.mysql.jdbc.Driver";
    String url();
    String username() default "root";
    String password() default "xxxxxx";
}

JDBCUtil.java

package org.westos.util;

import org.westos.demo.JDBCAnno;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author lwj
 * @date 2020/8/25 13:55
 */
public class JDBCUtil {
    private JDBCUtil() {}

    @JDBCAnno(url = "jdbc:mysql:///javaweb?useSSL=false")
    public static Connection getConnection() throws NoSuchMethodException, ClassNotFoundException, SQLException {
        //使用反射获取注解的值

        Method method = JDBCUtil.class.getDeclaredMethod("getConnection");
        //获取方法对象
        if (method.isAnnotationPresent(JDBCAnno.class)) {
            JDBCAnno annotation = method.getDeclaredAnnotation(JDBCAnno.class);
            //获取方法上的注解
            String driverClass = annotation.driverClass();
            String url = annotation.url();
            String username = annotation.username();
            String password = annotation.password();
            //获取连接
            Class.forName(driverClass);
            return DriverManager.getConnection(url, username, password);
        }
        return null;
    }
}

测试类

package org.westos.test;

import org.westos.util.JDBCUtil;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author lwj
 * @date 2020/8/25 14:07
 */
public class MyTest {
    public static void main(String[] args) {
        try {
            Connection connection = JDBCUtil.getConnection();
            System.out.println(connection);
            //com.mysql.jdbc.JDBC4Connection@4b9af9a9
        } catch (NoSuchMethodException | ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值