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
    评论
简单的利用javajs实现文件 package com.fendou.myString; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class FileUpload extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean a=ServletFileUpload.isMultipartContent(request); if(a){ FileItemFactory factory=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload (factory); Iterator items; try{ items=upload.parseRequest (request).iterator(); while(items.hasNext()){ FileItem item=(FileItem) items.next(); if(!item.isFormField()){ String name=item.getName (); String fileName=name.substring(name.lastIndexOf("\\")+1, name.length()); String path=request.getRealPath("file")+File.pathSeparatorChar+fileName; File uploadFile=new File (path); request.getSession ().setAttribute("file", uploadFile); item.write(uploadFile); response.setContentType ("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); // out.print("<font size='2'> 上文件为:"+name+"<br>"); // out.print("保存在服务器上 的地址为:"+path+"</font>"); } } }catch(Exception e){ e.printStackTrace(); } } response.sendRedirect("smartupload.jsp"); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值