我正在尝试为上传的文件编写一个java自定义标记。但是在上传文件时它返回null作为输出。任何帮助都可以欣赏
标记处理程序类:
public class SavePostTag extends SimpleTagSupport {
boolean isMultiPart;
public void doTag() throws JspException, IOException {
PageContext ctx = (PageContext)getJspContext();
HttpServletRequest request = (HttpServletRequest)ctx.getRequest();
try {
final Part tmpFile = request.getPart("file");
String fileName = request.getHeader("content-disposition");
ir.mahdiii.entity.File file = new ir.mahdiii.entity.File(Constant.repositoryPath + fileName, UrlGenerator.getInstance().generateRndUrl());
DbManager.getInstance().saveFileInfo(file);
FileOutputStream fileOutputStream = new FileOutputStream(file);
int tmp = 0;
FileInputStream fileInputstream = (FileInputStream)tmpFile.getInputStream();
while ((tmp = fileInputstream.read(new byte[1024])) != -1) {
fileOutputStream.write(tmp);
}
fileOutputStream.close();
} catch (ServletException | ClassNotFoundException | SQLException | NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getJspContext().getOut().write("SUCCESS");
}
JSP文件:
<c:if test="${param.status eq 'success'}">
<j:savepost/>
</c:if>
<div class="row">
<form action="/web/?page=admin&status=success" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
</div>
但在final Part **tmpFile = request.getPart("file");
行,它会返回null
。
任何想法?