老师,我的文件上传没反应,在判断是表单项还是文件上传项的时候没走else
package org.imooc.Servlet;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class ImportExcelServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Servlet成功调用");
if (ServletFileUpload.isMultipartContent(req)) {
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setHeaderEncoding("UTF-8");
try {
List<FileItem> fileItemList = upload.parseRequest(req);
for (FileItem fileItem : fileItemList) {
if (fileItem.isFormField()) {
System.out.println("是表单项");
System.out.println(fileItem.getFieldName() + "," + fileItem.getString("UTF-8"));
} else {
System.out.println("是文件上传项");
System.out.println(fileItem.getFieldName());
fileItem.write(new File("d:/Upload/" + fileItem.getName()));
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
正在回答
页面中上传文件项,给input框增加一个name后再试下~
即:<input type="file" name="excel" style="width:100%;"/>
祝学习愉快!
打印fileItemList的值如下
[name=null, StoreLocation=D:\Tomcat\apache-tomcat-9.0.8\temp\upload_656a418a_6fbb_4f55_9c4f_f88aa2ab58fb_00000000.tmp, size=12 bytes, isFormField=true, FieldName=title]
jsp代码如下:
<%@page language="java" contentType="text/html; UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE"/>
<title></title>
<link rel="stylesheet" type="text/css" href="css/all.css"/>
<link rel="stylesheet" type="text/css" href="css/pop.css"/>
<link rel="stylesheet" type="text/css" href="css/main.css"/>
</head>
<body style="background: #e1e9eb;">
<form id="mainForm" method="post" enctype="multipart/form-data" action="${basePath}/importExcelServlelt">
<div class="right">
<div class="current">当前位置:<a href="###">导入/导出</a> > 导入Excel</div>
<div class="rightCont">
<p class="g_title fix">导入</p>
<table class="tab1" width="100%">
<tbody>
<tr>
<td align="right" width="10%" name="title">标题<font color="red">*</font>:</td>
<td width="30%">
<input id="title" name="title" class="allInput" style="width:100%;" type="text"/>
</td>
<td align="right" width="10%" name="excel">选择文件<font color="red">*</font>:</td>
<td width="30%">
<input type="file" style="width:100%;"/>
</td>
</tr>
</tbody></table>
<div style="text-align: center; margin-top: 30px;">
<input class="tabSub" value="导 入" onclick="document.getElementById('mainForm').submit()" type="button" />
</div>
</div>
</div>
</form>
</body>
</html>
不会用断点
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星