java文件上传 easyui_文件上传至服务器(java+[easyUi+common])

html部分:

style="width: 350px; height:180px;" closed="true"

buttons="#upload-buttons" data-options="modal:true" >

style="font-size: 12px;">上传文件:

iconCls="icon-import" id="uploadBtn">上传

href="javascript:void(0)" class="easyui-linkbutton"

iconCls="icon-cancel"

οnclick="javascript:$('#uploadMapFiledlg').dialog('close')">取消

js代码:

//按钮部分

$(function(){

$("#uploadBtn").click(function(){

if($(this).linkbutton('options').disabled == false){

var file =$("#uploadfile").filebox('getValue');

if(file==""){

$.messager.alert('提示', '请先选择要上传文件!', 'info');

return;

}

var type=file.slice(file.lastIndexOf(".")+1,file.length);

var checkType=type.toUpperCase();

if(checkType!=="JSON"){

$.messager.alert('提示', '所选的文件格式不正确!', 'info');

return;

};

var uploadFileName=file.slice(file.lastIndexOf("\\")+1,file.length);

$.ajax({

type: "post",

url: contextPathName+"/reportInfo/cheackFile.do",

data: {"uploadFileName":uploadFileName},

dataType: "json",

success: function (data) {

if(data.success){

if(data.isExists){

//用户确定覆盖?

$.messager.confirm('确认对话框', data.fileMsg, function(r){

if (r){//覆盖(继续)

virtualUpload();

}else{ //取消

var content ="

上传结果:

";

content +="

您取消了文件的上传

";

$('#resultDiv2').html(content);

$('#uploadMapFileForm').form("reset");

return;

}

});

}else{//文件不存在

virtualUpload();

}

}

},

error:function(){

$.messager.alert('提示', "服务器发生错误!", 'info');

}

});

}

});

//显示上传地图文件窗口

$("#uploadMapFileBtn").click(function(){

if($(this).linkbutton('options').disabled == false){

$('#uploadMapFileForm').form("reset");

$('#resultDiv2').empty();

$('#uploadMapFiledlg').dialog('open').dialog('setTitle', '上传地图文件');

}

});

})

//实际上传操作

function virtualUpload(){

var intervalId = null;

var progressBar = null;

var options = {

dataType : "json",

beforeSubmit : function() {

$.messager.progress({

title:'请稍后',

msg:'正在处理中...',

});

},

complete:function(){

$.messager.progress('close');

},

success : function(data) {

if(data.success){

$.messager.alert('提示', "操作成功", 'info');

var content ="

上传结果:

";

content +="

"+data.resultMsg+"

";

$('#resultDiv2').html(content);

$('#uploadMapFileForm').form("reset");

}else{

$('#resultDiv2').empty();

$.messager.alert('提示', data.errorMsg, 'info');

}

},

error : function(result) {

$.messager.alert('提示', "服务器发生错误!", 'info');

}

};

$('#uploadMapFileForm').ajaxSubmit(options);

return !1;

}

java后台(控制器里的)

//检查文件是否存在

@RequestMapping ("/cheackFile")

@ResponseBody

public String cheackFile (HttpServletResponse response, HttpServletRequest request)

{

Map data = new HashMap ();

boolean result = Boolean.TRUE;

// 定义容器接收文件名

String uploadFileName = null;

try

{

String brProjectPath = request.getSession ().getServletContext ().getRealPath ("/");

uploadFileName = request.getParameter ("uploadFileName");

// 文件保存路径

String filePath = brProjectPath + "map\\" + uploadFileName;

File date = new File (filePath);

if (date.exists ())

{

data.put ("isExists", Boolean.TRUE);

data.put ("fileMsg", "文件已存在!是否覆盖!");

}

else

{

data.put ("isExists", Boolean.FALSE);

}

}

catch (Exception e)

{

_logger.error (e.getMessage (), e);

result = Boolean.FALSE;

data.put ("errorMsg", "文件上传失败!");

}

data.put ("success", result);

String jsonStr = JSONObject.fromObject (data).toString ();

return jsonStr;

}

//接收上传文件

@RequestMapping ("/uploadMapFile")

@ResponseBody

public String doUploadMapFile (@RequestParam (value = "uploadfile", required = false) MultipartFile uploadfile,

HttpServletRequest request)

{

Map data = new HashMap ();

boolean result = Boolean.TRUE;

// 定义容器接收文件名

String uploadFileName = null;

try

{

InputStreamReader read = new InputStreamReader (uploadfile.getInputStream (), "utf-8");// 考虑到编码格式

BufferedReader bufferedReader = new BufferedReader (read);

StringBuffer jsonContent = new StringBuffer ();

String content = "";

while ((content = bufferedReader.readLine ()) != null)

{

jsonContent.append (content);

}

String brProjectPath = request.getSession ().getServletContext ().getRealPath ("/");

uploadFileName = uploadfile.getOriginalFilename ();

// 文件保存路径

String filePath = brProjectPath + "map\\" + uploadFileName;

File date = new File (filePath);

FileOutputStream fop = null;

fop = new FileOutputStream (date);

byte[] contentInBytes = jsonContent.toString ().getBytes ();

fop.write (contentInBytes);

fop.flush ();

fop.close ();

read.close ();

}

catch (Exception e)

{

_logger.error (e.getMessage (), e);

result = Boolean.FALSE;

data.put ("errorMsg", "文件上传失败!");

}

data.put ("success", result);

data.put ("resultMsg", uploadFileName + "上传成功!");

String jsonStr = JSONObject.fromObject (data).toString ();

return jsonStr;

}

效果

点击上传按钮,弹出上传文件框

f7867a1183629cec5d3384fed75032db.png

选择文件:

555c64bd6aa633f03e247bc07f18b0de.png

ddd4365673b2ed9dca8a79dc2b4e02d5.png

操作提示:

a9b086ddf72111039bf13abd65947ee4.png

显示结果:

d43ed4ca3aae153637ae215a17148b55.png

检测到同名文件,给用户提示:

5d20bb950c70dcc666408a07b148bab3.png

取消上传后:

33be343c94d6b817e4f32fbf7e679451.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值