jfinal利用form表单同时上传图片和text

一:页面

<form class="form-horizontal" action="/users/upload" method="post" enctype="multipart/form-data" name="myform" id="myform">
<input hidden="hidden" name="userId" id="userId" readonly="readonly" style="display: none;">
<div class="form-group  form-group-lg">
   <label for="name">姓名:</label>
   <input type="text" onblur="checkName()" onchange="checkName()" class="form-control" name="name" id="name" placeholder="姓名">
   <br><span id="nameerr" style="padding-left:5rem;color: red;"></span>
</div>
<div class="form-group  form-group-lg">
   <label for="telephone">手机号码:</label>
   <input type="text" onblur="checkPone()" onchange="checkPone()" class="form-control" name="telephone" id="telephone" placeholder="手机号码">
   <br><span id="phoneerr" style="padding-left:5rem;color: red;"></span>
</div>
<div class="form-group">
   <label for="cardNo">身份证号:</label>
   <input type="text" onblur="checkNum()" onchange="checkNum()" class="form-control" name="cardNo" id="num" placeholder="身份证号">
<br><span id="numerr" style="padding-left:5rem;color: red;"></span>
</div>
<div class="form-group">
   <label for="cardFront">身份证件:</label>
   <input type="file" onchange="checkPic1()" name="cardFront" accept="image/*" class="form-control" id="pic1">
   <span class="file">上传手持身份证正面照片</span>
   <br><span id="pic1err" style="padding-left:5rem;color: red;"></span>
</div>
<div class="form-group">
   <label for="cardBehi">身份证件:</label>
   <input type="file" onchange="checkPic2()" accept="image/*" class="form-control" name="cardBehi" id="pic2">
   <span class="file">上传手持身份证背面照片</span>
   <br><span id="pic2err" style="padding-left:5rem;color: red;"></span>
</div>
<div class="form-group">
   <label for="special">特殊证书:</label>
   <input type="file" onchange="checkPic3()" accept="image/*" class="form-control" name="special" id="pic3">
   <span class="file">上传手持证书(有公章页)照片</span>
   <br><span id="pic3err" style="padding-left:5rem;color: red;"></span>
</div>
<button type="submit" class="btn btn-primary btn-info btn-block">立即提交</button>
</form>

二:代码

public void upload() throws UnsupportedEncodingException {
HttpServletRequest req = getRequest();
req.setCharacterEncoding("UTF-8");
String userId = "";
String telephone = "";
String cardNo = "";
String name = "";
DiskFileItemFactory disk = new DiskFileItemFactory();
File ff = new File("d:");
if (ff.exists()) {
disk.setRepository(ff);// 设置临时目录,用于保存临时文件
}
ServletFileUpload upload = new ServletFileUpload(disk);
try {
List<FileItem> list = upload.parseRequest(req);
List<Map<String, Object>> files = new ArrayList<>();
String path = req.getServletContext().getRealPath("/users");
for (int i = 0; i < list.size(); i++) {
FileItem item = list.get(i);
String paths = "";
if (i == 4) {
paths = userId + "cardfront.jpg";
} else if (i == 5) {
paths = userId + "cardbehi.jpg";
} else if (i == 6) {
paths = userId + "cardspeci.jpg";
} else {
paths = userId + "2.jpg";
}
item.write(new File(path, paths));
item.delete();
if (item.isFormField()) { // 此处是判断非文件域,即不是<inputtype="file"/>的标签
String n = item.getFieldName(); // 获取form表单中name的id
if ("userId".equals(n)) {
userId = item.getString("utf-8");
} else if ("telephone".equals(n)) {
telephone = item.getString("utf-8");
} else if ("cardNo".equals(n)) {
cardNo = item.getString("utf-8");
} else if ("name".equals(n)) {
name = item.getString("utf-8");
}
}
}
boolean boo = UserService.me.addSpecial(name, userId, telephone, cardNo);
} catch (Exception e) {
throw new RuntimeException(e);
}
renderJson(123);
}

三:结果




猜你喜欢

转载自blog.csdn.net/m0_37934074/article/details/78874968