更新代码后无法实现上传
severlet代码
if (ServletFileUpload.isMultipartContent(req)) {
FileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
servletFileUpload.setHeaderEncoding("UTF-8");
Date date =new Date();
List<FileItem> list = null;
try {
list = servletFileUpload.parseRequest(req);
for (FileItem fileItem : list) {
if (fileItem.isFormField()) {
//普通表单项没问题
}
} else {
//无法进入上传表单
DateFormat df1 = new SimpleDateFormat("yyyyMMddhhmmss");
Image.add(UId+df1.format(date));
File fDir = new File(File.separator);
String strFile = "D:\\FingerCoding\\src\\main\\webapp\\img\\ForumImage\\" + UId+df1.format(date);
File file=new File(strFile);
fileItem.write(file);
}
页面代码
<form action="issue" method="post" enctype="multipart/form-data" onsubmit="return Check()">
<div class="section">
<input class="upload" type="file" onchange=onUpload1(this.files[0])>
<div class="preview preview1"></div>
</div>
<script>
function onUpload1(file) {
var fr = new FileReader();
fr.readAsDataURL(file); // 将文件读取为Data URL
fr.onload = function (e) {
var result = e.target.result;
if (/image/g.test(file.type)) {
var img = $('<div class="photo"><img src="' + result + '"onload="ImageScale(this)"></div>');
$('.preview1').html('').append(img);
} else if (/video/g.test(file.type)) {
var vidoe = $('<div class="photo"><video controls src="' + result + '"><div>');
$('.preview1').html('').append(vidoe);
}
}
}
function ImageScale(obj) {
var ImageWidth = obj.offsetWidth;
var ImageHeight = obj.offsetHeight;
if (ImageWidth > ImageHeight) {
obj.style.width = "100px";
} else {
obj.style.height = "100px";
}
}
</script>
<input type="submit" value="发布">
<script>
function Check() {
<--!检查表单是否填写错误
var user = document.getElementById("UId").value;
var title = document.getElementById("Title").value+"";
var article = document.getElementById("Article").value+"";
if (user == ""||user=="null") {
alert("请先登录");
return false;
} else if (title == "") {
alert("请输入主题")
return false;
} else if (article == "") {
alert("正文不能为空");
return false;
}
} -->
</script>
</form>