图片的上传与下载

1、图片的上传

util

package com.qf.property.utils;

import java.io.File;
import java.util.UUID;

public class UploadUtils {
    //给文件重命名,因为同名会发生覆盖
    //后者给前者进行覆盖

    //传老名字然后生成一个新的名字  参数是老名字
    public static String newFileName(String fileName){
        //把随机生成的数中的-替换掉
        //因为UUID.randomUUID是生成一个26个字母加10个阿拉伯数字加-的随机数
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        //uuid生成的数+下划线+老的名字
        //因为fileName是原文件名它带着扩展名 .jpg .png等
        //而且原文件名将来会有用
        return uuid+"_"+fileName;
    }
    //给不同的文件动态创建文件夹
    //散列存储:不同文件存于不同的文件夹里
    //文件夹的名字是根据原文件的名字动态创建出来的
    //存储的二级路径、三级路径,根据文件名动态创建二、三级路径

    //调用此方法出现的是新的物理路径
    //参数一 basePath 是基本路径,也就是一级路径,对应物理路径,本例中一级路径代表的是web下的comimg
    //参数二 fileName 原文件名
    public static String newFilePath(String basePath,String fileName){
        //先得到原始文件名所对应的hashCode
        int hashCode=fileName.hashCode();
        //得到的hashCode和15做&(与)运算-----除了15别的也可以,但二级文件一般15个文件夹就够了所以与15做&运算
        //&(与)运算在二进制当中 相同都为1时是1,其余可能性都为0
        //计算后的结果就是二级路径的路径名也就是文件夹名  0~15之间16个文件夹
        int path2 = hashCode&15;  //任何数与15&得到的一定是0~15之间的数
        //三级路径 原来路径向右移四位
        int path3=(hashCode>>4)&15;
        String newPath=basePath+"\\"+path2+"\\"+path3;
        File file=new File(newPath);
        if (!file.exists()){
            file.mkdirs();
        }
        return newPath;
    }
}

com1---不考虑细节版本

package com.qf.property.servlets;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
//先获取name值是img的file标签,然后将其转换成物理路径,然后判断路径是否存在
//存在就判断获取的这个值是否有附件 然后上传 不存在就创建路径然后判断是否有附件然后上传
@WebServlet(name = "AddComServlet",value = "/addcom")
//针对于上传文件的注解 byte*kb*mb  前者是上传的大小是多少  后者是下载的大小是多少
@MultipartConfig(maxFileSize = 1024*1024*100,maxRequestSize = 1024*1024*100)
public class AddComServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //文件上传的功能
       //数据库并不存图片,图片存放在服务器的某个路径下(文件夹下)
       //数据库中存放的是文件的名字

        //获取表单数据  图片数据(流)


        //把图片(流)输出到服务器的某个路径下
        //知道文件的存储路径
        //1、存储路径-----把相对路径转换成物理路径(绝对路径)
        //2、重命名,防止覆盖 一定要唯一
        //3、散列存储,不同的图片存在不同的路径下,防止大量的图片存到同一路径下,查找效率低
        //4、输出流存储即可

        //先不考虑细节,上传图片
        //上传是有表单路径的  用part进行上传 img是前端的name
        //客户端提交的附件会随着http协议的响应体(post)传输到服务器
        //服务器使用part对象对附件进行了封装
        //获取name值是img的file标签
        Part part = request.getPart("img");
        //上传路径
        //文件存储必须使用物理路径,转换即可
        //request.getServletContext() 获取的是Servlet容器对象,
        // 相当于tomcat容器了。getRealPath("/") 获取实际路径,“/”指代项目根目录,所以代码返回的是项目在容器中的实际发布运行的物理路径
        //这一步是把相对路径转换成物理路径
        String path = request.getServletContext().getRealPath("/comimg");
        System.out.println("文件存储路径"+path);
        //文件要上传应该创建文件类
        File file = new File(path);
        //判断该路径是否存在,如果不存在则创建该路径
        //exists----代表存在
        //如果不存在
        if(!file.exists()){
            //创建路径
            file.mkdirs();
        }
        //判断用户是否选择了附件
        if(part!=null){
            //进行上传
            //路径加/加原来文件的名字
            //separator代表路径
            //目前放在tomcat里以后会转走因为放在tomcat里随着tomcat的重启图片会消失
            part.write(path+File.separator+part.getSubmittedFileName());
            //到上边位置文件就上传成功了
            System.out.println("文件上传成功!");
        }




        //调用业务逻辑

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

com2

age com.qf.property.servlets;

import com.qf.property.pojo.Community;
import com.qf.property.utils.UploadUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(name = "AddComServlet2",value = "/addcom2")
//针对于上传文件的注解 byte*kb*mb  前者是上传的大小是多少  后者是下载的大小是多少
@MultipartConfig(maxFileSize = 1024*1024*100,maxRequestSize = 1024*1024*100)
public class AddComServlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Part part = request.getPart("img");
        //源文件名
        String fileName = part.getSubmittedFileName();
        //文件名
        String newName = "";
        //创建对象
        Community community = new Community();
        if(fileName!=null){
            //调用工具得到新文件的名字
            newName = UploadUtils.newFileName(fileName);
            //赋文件名
            community.setImg(newName);
            //准备路径问题
            String path = request.getServletContext().getRealPath("/coming");
            //得到新的路径
            //参数一是一级路径
            //参数二是原文件名
            String newPath = UploadUtils.newFilePath(path,fileName);
            //测试输出
            System.out.println("新文件名" + newName);
            System.out.println("新路径" + newPath);
            //进行文件上传
            part.write(newPath+"\\"+newName);
            response.getWriter().write("文件上传成功");
        }
            //其他标签,就不是file的文件标签了
            String admin = request.getParameter("admin");
            String name = request.getParameter("name");
            String  loc = request.getParameter("loc");
            community.setAdmin(admin);
            community.setName(name);
            community.setLocation(loc);

    }
    //调用业务逻辑

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

前端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="bootstrap-3.4.1-dist/css/bootstrap-theme.css" />
    <link rel="stylesheet" href="bootstrap-3.4.1-dist/css/bootstrap-theme.min.css" />
    <link rel="stylesheet" href="bootstrap-3.4.1-dist/css/bootstrap.css" />
    <link rel="stylesheet" href="bootstrap-3.4.1-dist/css/bootstrap.min.css" />
    <script type="text/javascript" src="bootstrap-3.4.1-dist/js/jquery-3.4.1.min.js" ></script>
    <script type="text/javascript" src="bootstrap-3.4.1-dist/js/bootstrap.js" ></script>
    <script type="text/javascript" src="bootstrap-3.4.1-dist/js/bootstrap.min.js" ></script>
    <!--<script type="text/javascript" src="bootstrap-3.4.1-dist/js/npm.js" ></script>-->
    <script type="text/javascript" src="bootstrap-3.4.1-dist/js/bootstrap-dropdown.js" ></script>
        <style>
            .mybox{
                padding: 50px;
                width: 400px;
                margin: 0 auto;
            }
        </style>
</head>
<body>
<div class="container mybox">
<!--    method必须是post  enctype必须是multipart/form-data-->
    <form class="form-horizontal" method="post" enctype="multipart/form-data" action="addcom2">

        <div class="form-group">
            <label for="admin">管理员</label>
            <input type="text" id="admin" class="form-control" />
        </div>

        <div class="form-group">
            <label for="name">小区名称</label>
            <input type="password" id="name" class="form-control" />
        </div>

        <div class="form-group">
            <label for="location">小区位置</label>
            <input type="password" id="location" class="form-control" />
        </div>

        <div class="form-group">
            <label for="img">小区缩略图</label>
<!--            第三要素 type必须是file-->
            <input type="file" id="img" name="img" class="form-control">

        </div>

        <button class="btn btn-primary">保存小区信息</button>
    </form>
</div>
</body>
</html>

多文件的上传

package com.qf.property.servlets;

import com.qf.property.utils.UploadUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Collection;

//多文件上传
@WebServlet(name = "MultiServlet",value = "/multiservlet")
@MultipartConfig(maxFileSize = 1024*1024*300,maxRequestSize = 1024*1024*300)
public class MultiServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //路径
        String path= request.getServletContext().getRealPath("/comimg");
        File file = new File(path);
        if (!file.exists()){
            file.mkdirs();
        }
        //多文件上传-----获取到了多个附件
        Collection<Part> parts = request.getParts();
        //不为空证明有附件
        if (parts!=null){
            //挨个上传
            for(Part part:parts){
                //获取文件的名字
                String fileName=part.getSubmittedFileName();
                //通过判断是否获取到文件名能够知道当前标签是普通标签还是文件上传的标签
                if(fileName!=null){
                    //说明是文件标签  type="file"
                    if(fileName.trim().equals("")){
                        continue;
                    }
                    //获取新的文件名
                    String newName= UploadUtils.newFileName(fileName);
                    //获取到新的路径
                    String newPath=UploadUtils.newFilePath(path,fileName);
                    //存储
                    part.write(newPath+"\\"+newName);
                    System.out.println(fileName+"上传成功");
                }else{

                }

            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

前端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>多文件上传</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="multiservlet">
    <p>文件1:<input type="file" name="file1"/></p>
    <p>文件2:<input type="file" name="file2"/></p>
    <p>文件3:<input type="file" name="file3"/></p>
    <input type="submit" value="多文件上传"/>
</form>
</body>
</html>

文件的下载

util

package com.qf.property.utils;

import java.io.File;
import java.util.HashMap;

public class DownLoadUtils {
    //根据一级目录查找文件,把查找到的文件存储再集合里
    //参数一:查找的路径
    //参数二:存放查找到的文件名的集合
    public static void getFilesList(File file, HashMap<String,String> fileNames){
        //声明数组,存放当前目录下的所有内容:文件,文件夹
        File[] files = file.listFiles();
        if(files!=null){
            //遍历数组,逐个目录判断是否还有子文件
            for(File file1:files){
                //如果是文件夹,递归调用自己直到找到文件
                if(file1.isDirectory()){
                    //递归调用
                    getFilesList(file1,fileNames);
                }else{
                    //文件
                    //1、获得文件的名字
                    String fileName=file1.getName();
                    //获取到的是重命名之后的名字
                    int i=fileName.indexOf("_");
                    //获取到文件的源名字
                    //为了下载的时候,知道该文件的存储路径
                    String realName = fileName.substring(i+1);
                    //存入集合
                    fileNames.put(fileName,realName);
                }
            }
        }
    }

}

servlet

package com.qf.property.servlets;

import com.qf.property.utils.UploadUtils;

import javax.servlet.Servlet;
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.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

//文件的下载
@WebServlet(name = "DownImgServlet",value = "/downfile")
public class DownImgServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //完成下载功能
        //文件的根目录
        String path=request.getServletContext().getRealPath("/comimg");
        //获取要下载的文件名
        //filename是前端下载那里?后边的
        String fileName=request.getParameter("filename");
        //获取原文件名为了找路径
        //split是分割
        String realName=fileName.split("_")[1];
        //找图片的存储路径
        String realPath = UploadUtils.newFilePath(path,realName);


        //设置响应头信息,告知浏览器如何处理流,下载,另存信息----文件下载特有
        response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8"));

        //1、先按路径读取到文件
        FileInputStream fileInputStream = new FileInputStream(realPath+"/"+fileName);
        //2、把读取到的文件输出到客户端
        ServletOutputStream servletOutputStream=response.getOutputStream();
        byte[] buf = new byte[1024*4];
        int len=0;
        while((len=fileInputStream.read(buf))!=-1){
            servletOutputStream.write(buf,0,len);
        }
        //关闭资源
        servletOutputStream.close();
        fileInputStream.getChannel();
    }
}

前端

<%--
  Created by IntelliJ IDEA.
  User: poison
  Date: 2022/3/22
  Time: 15:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>文件列表</title>
    <style>
            table{
                width: 600px;
                border-collapse: collapse;
                text-align: center;
            }
            td{
                border: 1px solid black;
            }
    </style>
</head>
<body>
<h1>文件下载列表</h1>
<table>
    <tr>
        <td>文件名</td>
        <td>操作</td>
    </tr>
<%--    一个entry是一个键值对--%>
    <c:forEach items="${map}" var="entry">
        <tr>
            <td>${entry.value}</td>
            <td>
                <a href="downfile?filename=${entry.key}">下载</a>
            </td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值