Java中Stringjava中类型转换MultipartFile

没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!博客分类:
异常如下:
Failed to convert property value of type org.springframework.web.multipart.commons.CommonsMultipartFile to required type byte[]
nested exception is java.lang.IllegalArgumentException:
Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile]
to required type [byte] for property photo[0]: PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor]
returned inappropriate value of type [org.springframework.web.multipart.commons.CommonsMultipartFile]
起初我的代码是这么写的:
&input type="file" name="photo"/&
public class User {
private byte[]
//其他的字段略去
@RequestMapping("/store.html")
public String store(@ModelAttribute("user") User user,
HttpServletRequest request, @RequestParam("photo") MultipartFile file)
throws Exception {
// 文件上传
if (!file.isEmpty()) {
// 存入到服务器
String path = request.getSession().getServletContext().getRealPath(File.separator) + "upload" + File.separator
+ file.getOriginalFilename();
file.transferTo(new File(path));
userService.save(user);
return "redirect:list.html";
当初的想法是这样:使用SpringMVC提供的文件上传,用
&form:form action="${ctx }/user/store.html" modelAttribute="user" enctype="multipart/form-data"&
@ModelAttribute("user") User user
绑定表单参数,然后一个save()方法就插入一个对象,包括photo字段。
但是这样是行不通的。表单的字段photo,与User的photo看似对应,但是MultipartFile却无法解析。
所以这个“理想”的办法行不通。
所以做了一个“折中的方法”。(可能不是最佳方案,提供一个参考)
&input type="file" name="file"/&
注意:这个字段名不是实体类的属性名
@RequestMapping("/store.html")
public String store(@ModelAttribute("user") User user, HttpServletRequest request, @RequestParam("file
") MultipartFile file)
throws Exception {
// 文件上传
if (!file.isEmpty()) {
// 插入到数据库
user.setPhoto(file.getBytes());
// 存入到服务器
String path = request.getSession().getServletContext().getRealPath(File.separator) + "upload" + File.separator
+ file.getOriginalFilename();
file.transferTo(new File(path));
user.setRegisteredTime(new Date());
userService.save(user);
return "redirect:list.html";
想法就是:表单的文件字段名与实体类的字段名不一致,然后手动赋值。
说明:当初的想法就是文件上传到服务器,并且存到数据库(真正的不一定这么做)。
我总觉得这不是最好的办法。
浏览 23164
周一Monday
浏览: 284714 次
来自: 上海
感谢,很实用
虽然通过接口方式省了一些代码,但每一个实体还是要写一个DAO, ...
spring mvc demo教程源代码下载,地址:http: ...
xy2401 写道没有找到我想要的分页查询这里
System ...
真心简单是啊吧
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!错误背景:由于文件储存在第三方的服务器上,所有需要讲将接收到MultipartFile文件 转换为File 然后传输。(Spring MVC)
通过搜索引擎 &找到了以下两种方法。
&均要在先spring xml中声明。如下: 
&bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /&
如需添加最大,最小等范围控制,请自行百度参考。
方法一:强转
CommonsMultipartFile cf = (CommonsMultipartFile)
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File file = fi.getStoreLocation();
亲测有效。但是后期发现设置的问题 导致文件转换中错误,文件不可读从而导致 程序抛出 &is not a normal file&&异常。
因为错误出现的随机性很大,所以选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用&MultipartFile.transferto()方法 。
代码如下:
f=File.createTempFile("tmp", null);
file.transferTo(f);   f.deleteOnExit();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
亲测有效。
阅读(...) 评论()

我要回帖

更多关于 java中的强制类型转换 的文章

 

随机推荐