当前位置:网站首页>JSP实现文件上传功能的同时还要向后台传递参数
JSP实现文件上传功能的同时还要向后台传递参数
2022-07-28 05:24:00 【SCHLAU_tono】
解决JSP实现文件上传功能的同时还要向后台传递参数的问题
1. 问题阐述
Jsp实现文件上传时,可以与HTML中的<form>表单配合完成。但是表单的ENCTYPE="multipart/form-data", 这使得表单的数据不编码以原始数据流的形式提交。因此在Servlet中requeset.getParementer("name");的取值为NULL
1. 这篇博文对ENCTYPE的各个属性值进行了测试
2. 原理解释
2. 解决方法
我的文件中其他业务逻辑的code比较多,所以这里以菜鸟教程的JSP上传实例举例。
这段是Servlet中关于上传的核心代码:
try {
// 解析请求的内容提取文件数据
@SuppressWarnings("unchecked")
List<FileItem> formItems = upload.parseRequest(request);
if (formItems != null && formItems.size() > 0) {
// 迭代表单数据
for (FileItem item : formItems) {
// 处理不在表单中的字段
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
// 在控制台输出文件的上传路径
System.out.println(filePath);
// 保存文件到硬盘
item.write(storeFile);
request.setAttribute("message",
"文件上传成功!");
}
}
}
} catch (Exception ex) {
request.setAttribute("message",
"错误信息: " + ex.getMessage());
}
文件的数据不在表单中的字段里,因此文件的数据处理放在了if(!item.isFormField())的函数中,所以想要获取文本数据的方法就是处理表单中的字段,即添加在if(!item.isFormField())后添加else语句
else{
String fieldName = fi.getFieldName();
String value = fi.getString("UTF-8");//.getString("encType")乱码问题的解决
System.out.println("filedName:"+fieldName+"\tvalue: "+value);
}
测试:
前端HTML文件中相关代码
<form class="form-inline" role="form" method="POST" enctype="MULTIPART/FORM-DATA" action="UploadServlet">
<!--省略了上传代码-->
<div class="form-group">
<label for"">请输入测试</label>
<input type="text" name="olicytexttest" id="olicytexttest">
</div>
</form>
控制台的输出:
filedName:olicytexttest value: sadfasdfdsaf
filedName:policytext value: sadfsdafsdf
filedeName中获取的是控件的name属性,value中获取的是控件中的输入
数据流中文件数据在最前面,然后才是表单中的其他控件顺序
除此之外,该问题还有很多其他解决方法,我认为本文提到的这种解决方法较为简单方便,很多大神在CSDN列举过很多其他方法。
边栏推荐
- What is the AEM testpro cv100 and fluke dsx-8000 of category 8 network cable tester?
- Briefly introduce EMD decomposition, Hilbert transform and spectral method
- PT 基于Multi Voltage的Physical Aware
- Matlab simulation of radar imaging 3 - multi-target detection
- clickhouse建宽表多少列最合适?
- Cronbach’s α? Kmo coefficient? Factor load? The most understandable course of questionnaire reliability and validity analysis in history!!! (SPSS and AMOS)
- clickhouse聚合之探索聚合内部机制
- Bag of Tricks训练卷积网络的技巧
- IMS-FACNN(Improved Multi-Scale Convolution Neural Network integrated with a Feature Attention Mecha
- Mae mask self encoding is scalable learning
猜你喜欢

Matlab simulation of radar imaging 2 - pulse compression and windowing

Pycharm2019 set editor theme and default code

PLC的选型

Arduino reads the analog voltage_ How mq2 gas / smoke sensor works and its interface with Arduino

Matlab simulation of radar imaging 4 - range resolution analysis

An example of bill printing

Low power design -power switch

Selection of PLC

EXFO 730c optical time domain reflectometer only has IOLm optical eye to upgrade OTDR (open OTDR permission)

set_ false_ path
随机推荐
浅谈误码仪的使用场景?
当mysql表从压缩表变成普通表会发生什么
Weight decay
What is the AEM testpro cv100 and fluke dsx-8000 of category 8 network cable tester?
Electric fast burst (EFT) design - EMC series hardware design notes 4
AEM online product promotion conference - Cable certification tester
Detailed explanation of creepage distance and electrical clearance
VAN(DWConv+DWDilationConv+PWConv)
Random life-1
Hugging face 的入门使用
Ship detection in SAR image based on yolov5
clock tree分析实例
ICC2使用report_placement检查floorplan
Transformer self attention mechanism and complete code implementation
Varistor design parameters and classic circuit recording hardware learning notes 5
Perl入门学习(十一)文件操作
Talk about the "hybrid mode" of esxi virtual switch and port group
浅谈FLUKE光缆认证?何为CFP?何为OFP?
vi和vim命令
VS Code 基础配置与美化