Flex代码
//导入
private var selectFile:FileReference = new FileReference();;
private var request:URLRequest;
public function _import():void{
Security.allowDomain("*");
var textFile:FileFilter = new FileFilter("Files (*.xls)","*.xls");
selectFile.browse(new Array(textFile));
selectFile.addEventListener(Event.SELECT,selectHandler);
}
private function selectHandler(event:Event):void{
var upLoadFiles:FileReference = FileReference(event.target);
request = new URLRequest("http://localhost:8080/webgis/common/import.do");
request.method = "post";
request.data = "files="+upLoadFiles.name;
selectFile.upload(request);
}
<mx:Button label="导 入" click="_import()" width="60" height="20"/>
JAVA后台
@Controller(".do")
public class ImportAction {
@RequestMapping
public void execute(HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("run in import");
// 创建一个通用的多部分解析器.
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
// 设置编码
commonsMultipartResolver.setDefaultEncoding("utf-8");
// 判断是否有文件上传
if (commonsMultipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multipartRequest = commonsMultipartResolver
.resolveMultipart(request);
Iterator iter = multipartRequest.getFileNames();
//iter里面没有值
while (iter.hasNext()) {
MultipartFile file = multipartRequest.getFile((String) iter
.next());
if (file != null) {
File localFile = new File(file.getOriginalFilename());
file.transferTo(localFile);
}
}
}
}