当前位置:网站首页>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 .
边栏推荐
- 雷达成像 Matlab 仿真 1 —— LFM信号及其频谱
- Filter
- T-sne dimension reduction visualization
- Fluke dtx-1800 and its accessories dtx-cha002 channel adapter channel replacement RJ45 socket notes
- 雷达成像 Matlab 仿真 2 —— 脉冲压缩与加窗
- Exploration of Clickhouse aggregation internal mechanism of aggregation
- Ship detection in SAR image based on yolov5
- EMC experiment practical case ESD electrostatic experiment
- How many columns are the most suitable for Clickhouse to build a width table?
- Low power design isolation cell
猜你喜欢
随机推荐
Analysis of MOSFET damage at the moment of power failure of isolated power supply
Why should fluke dsx2-5000 network cable tester be calibrated once a year?
set_multicycle_path
小程序:生命周期
mysql删表不删库
PyTorch 学习笔记 2 —— About Tensor
详解安装msdn 2015及其注意事项
【学习笔记】驱动
Pycharm2019 set editor theme and default code
ICC2使用report_placement检查floorplan
Synopsys Multivoltage Flow
Shuffle Net_ v1-shuffle_ v2
Redhawk Dynamic Analysis
Overall understanding of PLC
Fluke dtx-sfm2 single mode module of a company in Hangzhou - repair case
set_ false_ path
Hugging face 的入门使用
一、ffmpeg录制音频为pcm文件
QT custom sliding button (beautiful and easy to use)
qt自定义滑动按钮(美观且使用方便)









