spring框架上傳文件

頁面代碼(使用的是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數組。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章