gpt4 book ai didi

java - 多部分表单 POST 提交请求获取损坏的文件

转载 作者:搜寻专家 更新时间:2023-11-01 03:21:53 25 4
gpt4 key购买 nike

我无法正确上传文件。

我的问题摘要:任何正在上传的文件(*.docx、*.pdf、*.jpg/png/bmp 等)在服务器端收到时都是损坏的。

我的环境:JSP + Spring 3 MVC + Java。

我尝试了不同的方法,包括 here 建议的一种方法由 BalusC,但失败了。 Original picture distorted after upload

这些是严重失败的示例上传。

我的代码: tempform.jsp

<form:form method="POST" acceptCharset="ISO-8859-15" action="submit.htm"
commandName="commandform" enctype="multipart/form-data" >
...
<input name ="file0[] type="file" id="file0" multiple>
...
<input type="submit" name="submit">

controller.java

@RequestMapping(value = "/submit", method = RequestMethod.POST)
public ModelAndView submitRequest(@ModelAttribute("commandform") Request req, HttpServletRequest request, HttpServletResponse response, ModelMap model){
try {
MultipartHttpServletRequest tempPart = (MultipartHttpServletRequest) httpReq;
//file being transported is original.jpg and is only one.
MultipartFile filePart = tempPart.getFile("file0[]");
String fileName1 = filePart.getOriginalFilename();
InputStream fileContent = filePart.getInputStream();

//printing file here in this step for debugging purpose. Using jpg type only for example purpose.
BufferedImage bImageFromConvert = ImageIO.read(fileContent);
ImageIO.write(bImageFromConvert, "jpg", new File(
"e:/mynewfile.jpg"));
//file is created at location but with distorted version as shown in image.
...
}catch(Exception ex){
...
}
}

我的疑问:内容类型是否对这种行为负责?我在逼CharacterEncodingFilter<init-param>值为 ISO-8859-15在我的 web.xml .我用过ISO-8859-15 jsp 页面中的编码也是如此,因为我也必须处理欧洲文本。欢迎任何帮助或指导。提前致谢。

最佳答案

我不认为问题出在 acceptCharset="ISO-8859-15"
我根据 Spring IO 网站上的入门指南整理了一个测试用例(使用 Spring boot ):Spring IO file upload example

我还为工作中的一个文件上传项目编写了一个 Spring 3 MVC Controller 。它类似于我在下面显示的示例。

使用这个 Spring boot 测试用例,我可以使用 UTF-8 和 ISO-8859-15 上传您的示例图像。它工作正常。当然,我没有像您一样使用 CharacterEncodingFilter
这是我的一些代码,因此您可以与您的代码进行比较。

希望对你有所帮助。

应用程序.java:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("128KB");
factory.setMaxRequestSize("128KB");
return factory.createMultipartConfig();
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

文件上传 Controller .java:

@Controller
public class FileUploadController {

@RequestMapping("/")
public String welcome() {
return "welcome";
}

@RequestMapping(value="/upload", method=RequestMethod.GET)
public @ResponseBody String provideUploadInfo() {
return "You can upload a file by posting to this same URL.";
}

@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name;
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}

welcome.jsp 片段:

<%-- 
<form:form action="upload" method="POST" acceptCharset="UTF-8" enctype="multipart/form-data" >
--%>
<form:form action="upload" method="POST" acceptCharset="ISO-8859-15" enctype="multipart/form-data" >
<table>
<tr>
<td>
<!-- <input type="hidden" name="action" value="upload" /> -->
<strong>Please select a file to upload :</strong> <input type="file" name="file" />
</td>
</tr>
<tr>
<td>Name: <input type="text" name="name"><br />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Upload"> Press here to upload the file!
</td>
</tr>
</table>
</form:form>

关于java - 多部分表单 POST 提交请求获取损坏的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28325361/

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