gpt4 book ai didi

java - HttpServletRequest 无法转换为 MultipartHttpServletRequest

转载 作者:太空宇宙 更新时间:2023-11-04 06:48:42 26 4
gpt4 key购买 nike

我正在尝试上传 Excel 文件并在控制台中打印每个单元格的内容。这是我的代码..

JSP:

                <c:url value="/my-account/readExcel" var="readExcel" />
<form:form action="${readExcel}" method="post" commandName="excelFileUploadForm" enctype="multipart/form-data">
<form:input id="fineName" path="fileName" type="file" />
<input type="submit" value="Uplaod" />
</form:form>

Excel文件上传表单:

 public class ExcelFileUploadForm
{
private MultipartFile fileName;

public MultipartFile getFileName()
{
return fileName;
}

public void setFileName(final MultipartFile fileName)
{
this.fileName = fileName;
}
}

Controller :

@RequestMapping(value = "/readExcel", method = RequestMethod.POST)
@RequireHardLogIn
public String readExcel(final HttpServletRequest request, final HttpServletResponse response) throws CMSItemNotFoundException,
IOException
{
final MultipartHttpServletRequest mpr = (MultipartHttpServletRequest) request;
final CommonsMultipartFile file = (CommonsMultipartFile) mpr.getFile("fileName");
read(file);
return "redirect:/my-account";
}

public void read(final CommonsMultipartFile inputFile) throws IOException
{
Workbook w;
try
{
w = Workbook.getWorkbook(inputFile.getInputStream());
// Get the first sheet
final Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines

for (int j = 0; j < sheet.getColumns(); j++)
{
for (int i = 0; i < sheet.getRows(); i++)
{
final Cell cell = sheet.getCell(j, i);
final CellType type = cell.getType();
if (type == CellType.LABEL)
{
System.out.println("I got a label " + cell.getContents());
}

if (type == CellType.NUMBER)
{
System.out.println("I got a number " + cell.getContents());
}

}
}
}
catch (final BiffException e)
{
e.printStackTrace();
}
}

当我通过浏览按钮上传文件并单击上传时。它给出了类型转换错误

HTTP Status 500 - Request processing failed; nested exception is java.lang.ClassCastException: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest

我不知道我哪里做错了。请帮忙。

最佳答案

请在您的 spring 配置(应用程序配置)中添加 Multipart Resolver,如下所示:

<bean id="multiPartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

关于java - HttpServletRequest 无法转换为 MultipartHttpServletRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23624688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com