模板上传
jsp页面代码如下:
<div id="select_condition">
<div class="select_condition_row" name="form1" >
数据类型:
<select style="width:110px;" id="select_con">
<option value="1000" οnclick="selectBusinessHall()" >营业厅</option>
<option value="1002" οnclick="selectVillageMessage()">4G参数</option>
<option value="1003" οnclick="selectBasestationInforplan()">基站信息规划</option>
<option value="1001" οnclick="showMr()">MR数据配置</option>
</select>
<span id="datePage" style="right:10px;position:absolute;"></span>
</div>
<div class="select_condition_row select_button" id="exim">
<span><a οnclick="improtExample()">模板上传</a></span>
<span><a οnclick="DownloadExample()">模板下载</a></span>
<span><a οnclick="imports()">导入</a></span>
</div>
<input type="file" id="file" name="" οnchange="fileChange(this)" style="FILTER: alpha(opacity=0); moz-opacity: 0; opacity: 0;" />
<input type="file" id="uploadifys" name="myfiles" multiple="multiple" οnchange="fileChanges(this)" style="FILTER: alpha(opacity=0); moz-opacity: 0; opacity: 0;" />
</div>
js代码如下:
/**
* 模板上传
*/
function improtExample(){
var ie = !-[1,];
if(ie){
$("#uploadifys").trigger('click').trigger('change');
}else{
$("#uploadifys").trigger('click');
}
}
/**
* 模板上传
* @param even
*/
function fileChanges(even){
var ss = document.getElementById("uploadifys").files;//js中取到其中对象
// var fileName=$("#uploadify")[0].files[0].name;
if((ss.length)!=0){
$.messager.confirm('提示','确定您要导入配置?',function(r){
if (r){
myMask.show();
// $('#uploadify').attr("name",$('#uploadify').val());
$.ajaxFileUpload({
url: path+'/SystemSiteController/importTemplateData.do',
secureuri: false,//异步
fileElementId: 'uploadifys',//上传控件ID
dataType: 'text',//返回的数据信息格式
success: function(data){
myMask.hide();
if(data == "<pre>true</pre>"){
$.messager.alert('提示',"上传模板成功");
}
}
})
}
});
}else{
$.messager.alert('提示',"请先选择文件!!");
}
}
controller代码如下:
/**
* 模板数据上传
* @param request
* @param response
* @throws IOException
*/
@RequestMapping("importTemplateData")
public void importTemplateData(HttpServletRequest request, HttpServletResponse response, @RequestParam MultipartFile[] myfiles) throws IOException {
logger.info("**********into method importData*************");
boolean isTrue = false;
// 设置上下文
// MultipartFile myfile = myfiles[1];
// System.out.println("name="+myfile.getName()+",size="+ myfile.getSize()+ ",OriginalFilename="+ myfile.getOriginalFilename() );
InputStream is = null;
FileOutputStream output = null;
for(int i=0;i<myfiles.length;i++){
MultipartFile myfile = myfiles[i];
String myfileName = myfile.getOriginalFilename();
try {
is = myfile.getInputStream();//MultipartFile转成输入流FileOutputStream
String path = (request.getSession().getServletContext().getRealPath("")).replace("\\", "/") +"systemPage/";
String name = myfile.getOriginalFilename();
System.out.println("******"+path+"********");
File file = new File(path, name);
System.out.println(path);
if (!file.exists()) {
file.createNewFile();
}
output = new FileOutputStream(file);
byte[] b = new byte[1024];
while (is.read(b) != -1) {
output.write(b);
b = new byte[1024];
}
} catch (IOException e) {
response.getWriter().print(isTrue);
}
}
try {
isTrue = true;
response.getWriter().print(isTrue);
output.close();
is.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}