js上传文件给java_js,java上传文件到服务器

页面

上传您的文件 :

enctype="multipart/form-data" method="post" id="fileForm" name="fileForm">

style="background: #3692FD;border-radius: 6px; width: 29%;height: 10%"

οnclick="subimtBtn()">

上传运行

js

//上传运行

function subimtBtn() {

$("#submitBtn").attr("disabled", true);

var fileName = $('#file').val();

console.log("fileName:" + fileName)

if (fileName == null || fileName == undefined || fileName == '') {

callModelErrorMessageBox("文件不能为空");

$("#submitBtn").attr("disabled", false);

return;

}

if (fileName.length <= 4) {

callModelErrorMessageBox("文件名称不对");

$("#submitBtn").attr("disabled", false);

return;

}

fileName = fileName.substring(fileName.length - 4, fileName.length);

console.log(fileName)

if (fileName != '.xls') {

callModelErrorMessageBox("文件格式需要是.xls");

$("#submitBtn").attr("disabled", false);

return;

}

var form = $("form[name=fileForm]");

$("#uploadTip").html("正在上传...");

var options = {

action: '/optimizationJob/uploadFile.action',

type: 'post',

success: function (data) {

var success = data.success;

var errMsg = data.errMsg;

if (success == "Y") {

console.log("上传成功,返回success=Y,errMsg:" + errMsg);

$("#uploadTip").html("上传成功");

} else {

console.log("上传失败,返回success=N,errMsg:" + errMsg);

$("#uploadTip").html("上传失败");

}

$("#submitBtn").attr("disabled", false);

},

error: function (data) {

console.log("上传失败,返回error");

$("#uploadTip").html("上传失败");

$("#submitBtn").attr("disabled", false);

}

};

form.ajaxSubmit(options);

}//subimtBtn

java

private File file;

@Action(value = "uploadFile")

public void uploadFile() throws Exception {

//先创建本地文件夹

String filePath = "C:\\";

LOG.info("filePath: " + filePath);

//开始保存到本地

String fileName = getLoginErp() + "_" + DateUtil.formatYyyyMMddHHmmss(new Date()) + ".xls";

try {

OutputStream os = new FileOutputStream(new File(filePath + fileName));

InputStream is = new FileInputStream(file);

LOG.info("fileName: " + fileName);

byte[] buffer = new byte[10240];

while (-1 != (is.read(buffer, 0, buffer.length))) {

os.write(buffer);

}

os.close();

is.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

resultMap.put("errMsg", "上传文件异常");

resultMap.put("success", "N");

Gson gson = new Gson();

String result = gson.toJson(this.resultMap);

log.info("上传文件的响应数据:" + result);

response.setContentType("text/json;charset=UTF-8");

response.getWriter().write(result);

} catch (IOException e) {

e.printStackTrace();

resultMap.put("errMsg", "上传文件异常");

resultMap.put("success", "N");

Gson gson = new Gson();

String result = gson.toJson(this.resultMap);

log.info("上传文件的响应数据:" + result);

response.setContentType("text/json;charset=UTF-8");

response.getWriter().write(result);

}

resultMap.put("errMsg", "上传文件成功");

resultMap.put("success", "Y");

Gson gson = new Gson();

String result = gson.toJson(this.resultMap);

log.info("上传文件的响应数据:" + result);

response.setContentType("text/json;charset=UTF-8");

response.getWriter().write(result);

}//uploadFile

public File getFile() {

return file;

}

public void setFile(File file) {

this.file = file;

}

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值