文件上传案例

Servlet文件上传案例

文件上传注解:
@MultipartConfig(maxFileSize = 10241024100,maxRequestSize = 10241024200)
maxFileSize:单个文件的最大上传大小
maxRequestSize:多个文件的最大上传大小
获取请求数据
Path path=request.getPath(“前端自定义的数据名”);
获取上传文件的文件名
String sub = file.getSubmittedFileName();
上传文件存放的位置
String realPath = req.getServletContext().getRealPath("/WEB-INF/upload");

。防止文件覆盖
UUID.randomUUID().toString:得到一个字符串的唯一标识符
UUID.randomUUID().toString.replaceAll(regex:"_",replacement:" ")
将regex中的字符替换成replacement中的字符。

。散列存储
为了防止一个目录下标识码,要使用hash算法生成二,三级目录
int hashPath = wubPath.hashCode();得到hash值
int Path01=hashPath&7;
int Path02=(hashPath>>4)&7;
注意事项:
与运算的值后的数字,必须是二进制全为一的数

文件类型限制
文件名分割:"文件名".substring(int baginIndex)
作用:返回一个有索引baginIndex到结尾
“文件名”.lostIndexOf(“指定分割标记”);
contains方法
一个Object包含另一个Object:Object.contains(Object)
返回值:Boolean类型的值

@WebServlet(value = {"/File"})
@MultipartConfig(maxFileSize = 1024*1024*100,maxRequestSize = 1024*1024*200)
public class FileContext extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        Part file = req.getPart("file");
        //获取长传文件名称   如 a.java
        String submittedFileName = file.getSubmittedFileName();
        //格式判断
        if(!UUIDFile.Place(submittedFileName)){
            resp.getWriter().println("文件格式错误");
            return;

        }else {
            //防覆盖
            String s = UUIDFile.randomFile(submittedFileName);
            System.out.println(submittedFileName);
            String realPath = req.getServletContext().getRealPath("/WEB-INF/upload");
            //散列存储
            String storage = UUIDFile.storage(submittedFileName, realPath);
            //创建文件夹
            File file1=new File(storage);
            if(!file1.exists()){
                file1.mkdirs();
            }
            //文件写入
            file.write(storage+"\\"+s);
            resp.getWriter().println("上传成功");
        }
    }
}

public class UUIDFile {
    //防止文件覆盖
    public static String randomFile(String path){
        //
        String s = UUID.randomUUID().toString().replaceAll("_","")+"_"+path;
        return s;
    }
    //文件类型限制
    public static boolean Place(String path){
        String substring = path.substring(path.lastIndexOf("."));
        ArrayList<String> list=new ArrayList<>();
        list.add(".png");
        list.add(".jpg");
        list.add(".gif");
        list.add(".jpeg");
        if(list.contains(substring)){
            return true;
        }else {
            return false;
        }
    }
    //散列存储
    public static String storage(String wubPath,String path){
        int hashPath = wubPath.hashCode();
        int Path01=hashPath&7;
        int Path02=(hashPath>>4)&7;
        String Path=path+"\\"+Path01+"\\"+Path02;
        return Path;
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值