当前位置:网站首页>JSP should pass parameters to the background while realizing the file upload function
JSP should pass parameters to the background while realizing the file upload function
2022-07-28 06:33:00 【SCHLAU_ tono】
Catalog
solve JSP The problem of transferring parameters to the background while realizing the file upload function
1. Explain the problem
Jsp When uploading files , It can be done with HTML Medium <form> The form is completed . But the form's ENCTYPE="multipart/form-data", This makes the data of the form No coding Submit in the form of raw data stream . So in Servlet in requeset.getParementer("name"); The values for NULL
1. This blog post is right for ENCTYPE All attribute values of are tested
2. Explanation of principle
2. resolvent
Other business logic in my document code More , So here we are For beginners JSP Upload instance give an example .
This is Servlet About uploading the core code :
try {
// Parse the content of the request, extract the file data
@SuppressWarnings("unchecked")
List<FileItem> formItems = upload.parseRequest(request);
if (formItems != null && formItems.size() > 0) {
// Stack represents single data
for (FileItem item : formItems) {
// Handle fields that are not in the form
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
// The upload path of the output file in the console
System.out.println(filePath);
// Save the file to the hard disk
item.write(storeFile);
request.setAttribute("message",
" File upload succeeded !");
}
}
}
} catch (Exception ex) {
request.setAttribute("message",
" error message : " + ex.getMessage());
}
The data of the file is not in the fields in the form , Therefore, the data processing of the file is placed in if(!item.isFormField()) The function of , So the way to get text data is to deal with the fields in the form , That is, add in if(!item.isFormField()) Add else sentence
else{
String fieldName = fi.getFieldName();
String value = fi.getString("UTF-8");//.getString("encType") The solution of garbled code problem
System.out.println("filedName:"+fieldName+"\tvalue: "+value);
}
test :
front end HTML Relevant codes in the document
<form class="form-inline" role="form" method="POST" enctype="MULTIPART/FORM-DATA" action="UploadServlet">
<!-- Omitted upload code -->
<div class="form-group">
<label for""> Please enter the test </label>
<input type="text" name="olicytexttest" id="olicytexttest">
</div>
</form>
Console output :
filedName:olicytexttest value: sadfasdfdsaf
filedName:policytext value: sadfsdafsdf
filedeName Get the control's name attribute ,value Get the input in the control
The file data in the data stream is front , Then there is the order of other controls in the form
besides , There are many other solutions to this problem , I think the solution mentioned in this article is relatively simple and convenient , Many great gods are CSDN Many other methods have been listed .
边栏推荐
- My notes
- Briefly introduce EMD decomposition, Hilbert transform and spectral method
- Talk about the "hybrid mode" of esxi virtual switch and port group
- error: redefinition of ‘xxx‘
- I heard that you are also practicing when I interviewed several junior interns.
- How to calibrate dsx2-8000? Calibration process?
- Vs code basic configuration and beautification
- Paper artifact vs code + latex + latex workshop
- Matlab simulation of radar imaging 3 - multi-target detection
- 使用wampserver3.2.6时切换中文时造成启动失败
猜你喜欢

USB network native driver for esxi updated to support esxi7.0.1

set_ false_ path

Shuffle Net_ v1-shuffle_ v2

What about the insufficient memory of Clickhouse aggregation? Then improve the polymerization performance

Mae mask self encoding is scalable learning

听说你也在实习|当我采访了几个大三实习生之后。

EfficientNET_ V1

qt设置加载界面的几种方法

Matlab simulation of radar imaging 2 - pulse compression and windowing

Bag of tricks training convolution network skills
随机推荐
多个ics日历合并成单个ics日历
【学习笔记】工具
EMC experiment practical case ESD electrostatic experiment
qt自定义滑动按钮(美观且使用方便)
ubuntu mysql 设置远程访问权限
How to calibrate dsx2-8000? Calibration process?
Bag of tricks training convolution network skills
set_ clock_ groups
我的部署笔记
Word自动目录字体修改和行间距的问题
ICC2使用report_placement检查floorplan
Matlab simulation of radar imaging 1 - LFM signal and its spectrum
小程序:生命周期
当mysql表从压缩表变成普通表会发生什么
Graduation thesis | how to write literature review
[yolov5] environment construction: win11 + mx450
set_ case_ analysis
Perl入门学习(十)格式化输出
保研面试中常见的英语问题有哪些?
qt批量操作控件,并设置信号槽