<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