spring框架上传文件

来源:互联网 发布:飞行员燃料走淘宝 编辑:程序博客网 时间:2024/05/22 13:19

页面代码(使用的是freemark页面):

主要代码:

<form name="createApp" action="new.html" method="post" enctype="multipart/form-data">
<pre name="code" class="html">应用logo:<input type = "file" name = "appLogo">

<body>    <p>-------------能力包信息列表-----------------</p> <#list allPackets as packet> <p>能力包编号:           ${packet.code} 能力包名称:                   ${packet.name} 能力包描述:                   ${packet.desc} </p></#list><br/><form name="createApp" action="new.html" method="post" enctype="multipart/form-data">应用名:<input type = "text" name="appName"/>应用描述:<input type = "text" name = "appDesc">回调地址:<input type = "text" name = "callback">申请人姓名:<input type = "text" name = "appCName">申请人联系方式:<input type = "text" name = "appCPhone">应用logo:<input type = "file" name = "appLogo"><br/>    <#list allPackets as packet>    <input type="checkbox" name="packetCodes" value="${packet.code}"/>能力包编号:${packet.code}<br/>    </#list>    <input type="submit" name="submit" value="提交"></form></body>

<#list allPackets as packet>    <input type="checkbox" name="packetCodes" value="${packet.code}"/>能力包编号:${packet.code}<br/>    </#list>
上面这行代码的作用是列出能力包数组,然后选择列出选择框,将选择的能力包Code数组提交。

下面是接受form表单传输的数据:在viewCreateApp类中定义一个List<String>数组就可以接受能力包Code数组。

@RequestMapping(value = "/new",method = RequestMethod.POST)public String newApp(ViewCreateApp viewCreateApp) throws IOException{/*InputStream input = appLogo.getInputStream();String logoPath = "/opt/attachment/1.jpg";//TODO viewCreateApp.setLogoPath(logoPath);String partnerCode = "";//TODO 添加partnerCode从session中获取的。*/Date date = new Date();    byte[] appLogoData =   viewCreateApp.getAppLogo().getBytes();viewCreateApp.setCreateDate(date);viewCreateApp.setStatus(AppStatus.s0);viewCreateApp.setLogoPath("/12.jpg");AppService.createAttach("20",appLogoData);//Boolean isCreate = AppService.create(viewCreateApp);////if (isCreate) {//return "redirect:createAppSuccess";//} else {//return "redirect:createAppFailure";//}return "redirect:createAppSuccess";}
在viewCreateApp类中定义MultipartFile 文件来接受上传的文件
private MultipartFile appLogo;

 byte[] appLogoData =   viewCreateApp.getAppLogo().getBytes();
这样就将上传的文件转化成byte数组。

0 0
原创粉丝点击