文件上传分析
- 普通表单提交默认enctype=“application/x-www-form-urlencoded”;但是当表单中存在文件类型时,需要设置enctype=“multipart/form-data”,它不对字符进行编码。会导致上传失败
- 表单请求方式必须为post,不能为get。
传输有限制 一般认为2k 不同浏览器 不一样
如果你文件小于1k base64后成字符串 足可以使用get传文件
post使用httpbody 无限制
由于是文件上传,大小不一定 最好使用post!!
- 添加文件上传后,编码格式发送了变化,不能用getParameter来获取参数了.而是request.getInputStream()来进行解析
FileUpload
FileUpload分析
在使用FileUpload之前会进行导包
链接:https://pan.baidu.com/s/1WnsatQhSM2wKt0Ef8Lp9hg
提取码:mgk2
复制这段内容后打开百度网盘手机App
使用前奏
1、先建立一个工厂
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
2、创建解析
FileUpload fileUpload = new FileUpload(diskFileItemFactory);
3、存到list集合进行读取
List<FileItem> list = fileUpload.parseRequest(req);
表单提交的jsp代码
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6" id="form">
<h2>添加</h2>
<form class="form-horizontal col-md-12" id="fm" action="<%=contextPath%>/stu?a=add" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">姓名</label>
<div class="col-sm-5">
<input type="text" name="name" class="form-control" id="name" placeholder="姓名">
</div>
</div>
<div class="form-group">
<label for="age" class="col-sm-2 control-label">年龄</label>
<div class="col-sm-5">
<input type="number" name="age" class="form-control" min="1" max="255" id="age"
placeholder="年龄">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">性别</label>
<div class="col-sm-5">
<div class="radio">
<label>
<input type="radio" name="gender" id="man" value="1" checked>
男
</label>
<label>
<input type="radio" name="gender" id="woman" value="2" checked>
女
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="tel" class="col-sm-2 control-label">电话</label>
<div class="col-sm-5">
<input type="text" name="tel" class="form-control" id="tel" placeholder="电话">
</div>
</div>
<div class="form-group">
<label for="tel" class="col-sm-2 control-label">班级</label>
<div class="col-sm-5">
<select class="form-control" name="class_id">
<option>10001</option>
</select>
</div>
</div>
<div class="form-group">
<label for="tel" class="col-sm-2 control-label">照片</label>
<div class="col-sm-5">
<input type="file" name="pic" class="form-control" id="pic" placeholder="电话">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">添加</button>
<button type="reset" class="btn btn-warning">重置</button>
</div>
</div>
</form>
</div>
<div class="col-md-3"></div>
</div>
</div>
表单提交UploadServlet的代码( 缺陷并未去 验证 大小、类型 等是否符合要求)
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
FileUpload fileUpload = new FileUpload(diskFileItemFactory);
Map<String, String> map = new HashMap<>();
try {
List<FileItem> list = fileUpload.parseRequest(req);
for (FileItem fileItem : list) {
//判断是否是 普通的表单项
if (fileItem.isFormField()) {
map.put(fileItem.getFieldName(), fileItem.getString());
} else {//说明是 文件上传的表单项,处理 上传文件,将上传文件 保存到服务器的硬盘中
//拼接路径
String path = this.getServletContext().getRealPath("/") + "upload" + File.separator;
String fileName = UUID.randomUUID().toString();
String suffix = fileItem.getName().substring(fileItem.getName().lastIndexOf('.'));
//TODO 验证 大小、类型 等是否符合要求
File file = new File(path + fileName + suffix);
fileItem.write(file);
//将 路径 存储到 map ,以再便存到数据库里
map.put(fileItem.getFieldName(), "/upload/" + fileName + suffix);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
注意:
1、在进行提交图片是为了防止图片因为名字一样会代替掉原来的图片,就会利用利用UUID生成伪随机字符串作为文件名避免重复:UUID.randomUUID().toString();
2、将文件写到硬盘上。写完之后,系统会自动将放在临时文件目录的该文件删除:fileItem.write(new File(path,fileName));