我使用Spring(4)MVC和Hibernate(4)在Eclipse中创建动态Web项目,我想将文件的数据(文件将包含大量电话号码)上传到mysql中的表,所以如何读取文件数据并上传?
答案 0 :(得分:0)
请参阅以下链接
答案 1 :(得分:0)
您可以在Spring MVC中使用org.springframework.web.multipart.MultipartFile
上传文件
在Controller中添加方法
@RequestMapping(value = "/processUpload", method = RequestMethod.POST)
public @ResponseBody RateResponse<String> processContractUpload(@RequestParam("txtFile") MultipartFile uploadedFile) throws IOException, InterruptedException{
String result = "failure";
String outFile = "C:\\temp\file.txt"
if(file != null && file.getSize()>0){
byte[] bytes = file.getBytes();
BufferedOutputStream stream = null;
try{
stream = new BufferedOutputStream(new FileOutputStream(outFile));
stream.write(bytes);
result = "success";
}finally {
if(stream!=null){
stream.close();
}
}
}
return result;
}
HTML表单
<form id="uploadContractForm" name="uploadContractForm" action="processUpload" method="POST" enctype="multipart/form-data">
<table class="tableStyle">
<tr>
<td><input type="file" name="txtFile" id="txtFile" style="background:white;" class="summaryTextSpan4"/></td>
<td> </td>
<td><input id="processbtn" type="button" class="btn" value='Submit' /></td>
<td><input id="refreshBtn" class="btn" type="button" value='Reset' /></td>
</tr>
</table>
启用Spring Multipart
所需的Spring bean配置<!-- File Upload Configuration Bean Details -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1000000000" />
</bean>