jQuery+MultipartFile上传文件

6 篇文章 0 订阅
5 篇文章 0 订阅
<input type="file" name="filename" id="filename" required	>
<button type="button" onclick="saveAddControl();">提交</button>
function saveAddControl() {

	var prodName = $("#prodName").val();
	var conName = $("#conName").val();
	var versionName = $("#versionName").val();
	var descriptName = $("#descriptName").val();
	var formData = new FormData();
	formData.append("filename", $("#filename")[0].files[0]);
	$.ajax({
		url : '/version/saveAddControl?prodName=' + prodName + '&conName='
				+ conName + '&versionName=' + versionName + '&descriptName='
				+ descriptName,
		type : 'post',
		data : formData,
		contentType : false,
		processData : false,
		dataType : 'json',
		success : function(data) {
			// alert(res);
			var msg = data.msg;
			if (msg == "success") {
				alert('上传成功!');
				document.getElementById('contes').style.display = "none";
				window.location.href = "/version/toVersion"
			} else if (msg == "fail") {
				alert('版本已经存在,请重新填写版本名称!');
			} else {
				alert('上传失败,请重新上传!');
			}
		}
	})
}
/**
	 * @Title:
	 * @Description: 保存版本信息
	 * @param
	 * @return
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/saveAddControl", method = { RequestMethod.POST, RequestMethod.GET })
	public String saveAddControl(@RequestParam(value = "prodName", required = false) String prodName,
			@RequestParam(value = "conName", required = false) String conName,
			@RequestParam(value = "versionName", required = false) String versionName,
			@RequestParam(value = "descriptName", required = false) String descriptName,
			@RequestParam(name = "filename") MultipartFile multipartFile, HttpServletRequest request) throws Exception {
		// 1、获取原文件后缀名
		String originalFileName = multipartFile.getOriginalFilename();

		// 我上传至ftp服务器,你可以按照你自己的方式去存储。如果保存到服务器中,在我其他的文章有写,自己拿来合并。
		if (FileUtil.uploadToFtp(multipartFile.getInputStream(), "192.168.30.65", "root", "123456",
				"ftpFile\ftpService", originalFileName)) {
			System.out.println("上传至ftp服务器!");
		} else {
			System.out.println("上传至ftp服务器失败!");
		}
		JSONObject jsonObject = new JSONObject();

		//产品类型id
		int id = versionMapper.getId(prodName);
		//控制器id
		int controlId = versionMapper.getcontrolId(conName);
		//查看版本信息是否存在
		int count = versionMapper.exist(id, versionName, controlId);

		// 数据库中已经存在
		if (count > 0) {
			jsonObject.put("msg", "fail");
		} else {
			Version version = new Version();
			version.setDescript(descriptName);
			version.setProductTypeId(id);
			version.setVersion(versionName);
			version.setControlId(controlId);
			version.setFtpName(originalFileName);
			versionMapper.saveControl(version);
			jsonObject.put("msg", "success");
		}

		return jsonObject.toString();
	}

参考文献https://blog.csdn.net/qq_43097393/article/details/82218585

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值