JAVA_文件上传(html,js,后台逻辑)
来源:互联网 发布:报税软件 编辑:程序博客网 时间:2024/05/16 10:17
HTML:
<form id="uploadFiles" method="post" enctype="multipart/form-data">
<table>
<tr>
<td style="width:12%;text-align:center;vertical-align:top"><a style="color:red">*</a>附件上传:</td>
<td style="width:52%">
<input id="scromFile" type="file" name="uploadFile" />
<input type="submit" onclick="uploadFiles()" value="上传" class="fileup" />
</td>
</tr>
</table>
</form>
JS:
function uploadFiles(){
$('#uploadFiles').form(
{
url : baseUrl+'/downloadCenter/uploadFiles',
onSubmit : function() {
},
success : function(data) {
var obj=JSON.parse(data);
if (obj.result == "false") {
$.messager.alert('提示信息', '上传失败', 'warning');
} else if(obj.result=="size"){
$.messager.alert('提示信息', '文件过大', 'warning');
}else if(obj.result=="null"){
$.messager.alert('提示信息', '上传不能为空', 'warning');
}
else{
$.messager.alert("提示信息", "上传成功", "info", function(data) {
});
}
},
error : function(date) {
}
});
}
后台逻辑:
Controller:
@RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
public void uploadFile(MultipartFiles multipartFiles, HttpServletRequest request,HttpServletResponse resp)throws Exception {
Map<String,Object> map= downloadCenterService.uploadFile(multipartFiles,request);
try {
resp.setContentType("text/plain; charset=UTF-8");
resp.getWriter().write(JSONObject.toJSONString(map));
} finally {
resp.getWriter().close();
}
}
Service:
public Map<String, Object> uploadFile(MultipartFiles multipartFiles, HttpServletRequest request) throws Exception {
MultipartFile uploadFile = multipartFiles.getUploadFile();
Map<String, Object> map = new HashMap<>();
// 声明标识符
boolean flag = false;
if (uploadFile.getSize() != 0) {
flag = true;
}
if (!flag) {
map.put("result", "null");
return map;
}
// 如果校验成功,重置标志位
flag = false;
Long imgSize = IMG_SIZE;
// 校验图片大小
if (uploadFile.getSize() > imgSize) {
map.put("result", "size");
return map;
} else {
flag = true;
}
if (flag) {
String fileName = uploadFile.getOriginalFilename(); // 文件名
String realPath = DOWNLOAD_CENTER_UPLOAD + ""; // 文件上传路径
String saveFile = DOWNLOAD_CENTER_SAVE + "/" + fileName; // 存入数据库中的路径
File targetFile = new File(realPath, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
// 文件转存
uploadFile.transferTo(targetFile);
map.put("result", saveFile);
map.put("name", fileName);
map.put("size", uploadFile.getSize() / 1024);
return map;
} catch (Exception e) {
e.printStackTrace();
}
}
map.put("result", "false");
return map;
}
<form id="uploadFiles" method="post" enctype="multipart/form-data">
<table>
<tr>
<td style="width:12%;text-align:center;vertical-align:top"><a style="color:red">*</a>附件上传:</td>
<td style="width:52%">
<input id="scromFile" type="file" name="uploadFile" />
<input type="submit" onclick="uploadFiles()" value="上传" class="fileup" />
</td>
</tr>
</table>
</form>
JS:
function uploadFiles(){
$('#uploadFiles').form(
{
url : baseUrl+'/downloadCenter/uploadFiles',
onSubmit : function() {
},
success : function(data) {
var obj=JSON.parse(data);
if (obj.result == "false") {
$.messager.alert('提示信息', '上传失败', 'warning');
} else if(obj.result=="size"){
$.messager.alert('提示信息', '文件过大', 'warning');
}else if(obj.result=="null"){
$.messager.alert('提示信息', '上传不能为空', 'warning');
}
else{
$.messager.alert("提示信息", "上传成功", "info", function(data) {
});
}
},
error : function(date) {
}
});
}
后台逻辑:
Controller:
@RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
public void uploadFile(MultipartFiles multipartFiles, HttpServletRequest request,HttpServletResponse resp)throws Exception {
Map<String,Object> map= downloadCenterService.uploadFile(multipartFiles,request);
try {
resp.setContentType("text/plain; charset=UTF-8");
resp.getWriter().write(JSONObject.toJSONString(map));
} finally {
resp.getWriter().close();
}
}
Service:
public Map<String, Object> uploadFile(MultipartFiles multipartFiles, HttpServletRequest request) throws Exception {
MultipartFile uploadFile = multipartFiles.getUploadFile();
Map<String, Object> map = new HashMap<>();
// 声明标识符
boolean flag = false;
if (uploadFile.getSize() != 0) {
flag = true;
}
if (!flag) {
map.put("result", "null");
return map;
}
// 如果校验成功,重置标志位
flag = false;
Long imgSize = IMG_SIZE;
// 校验图片大小
if (uploadFile.getSize() > imgSize) {
map.put("result", "size");
return map;
} else {
flag = true;
}
if (flag) {
String fileName = uploadFile.getOriginalFilename(); // 文件名
String realPath = DOWNLOAD_CENTER_UPLOAD + ""; // 文件上传路径
String saveFile = DOWNLOAD_CENTER_SAVE + "/" + fileName; // 存入数据库中的路径
File targetFile = new File(realPath, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
// 文件转存
uploadFile.transferTo(targetFile);
map.put("result", saveFile);
map.put("name", fileName);
map.put("size", uploadFile.getSize() / 1024);
return map;
} catch (Exception e) {
e.printStackTrace();
}
}
map.put("result", "false");
return map;
}
阅读全文
0 0
- JAVA_文件上传(html,js,后台逻辑)
- webuploader+springmvc实现多文件上传(html+js+css原创,后台代码借鉴)
- webuploader+springmvc实现多文件上传(html+js+css原创,后台代码借鉴)
- Java_文件上传与下载(一)
- Java_文件上传与下载(二)
- Java_文件上传与下载(三)
- js上传文件 java后台接收文件
- js上传文件 java后台接收文件
- js+html 异步上传文件
- Uploadfile 控件上传文件的使用方法(js+jsp+后台)
- Vue.js 上传文件(后台使用.net)
- springMvc+swfUpload实现异步文件上传(前台用纯html后台返回json)
- 关于前端html文件上传和后台接收方法
- 31.上传文件到后台ajaxfileupload.js的使用
- 文件上传ajaxFileUpload.js使用方法 包含后台接收方法
- 文件上传ajaxFileUpload.js使用方法 包含后台接收方法
- PhoneGap 文件上传(Java 后台代码)
- 文件上传的后台
- window.open和window.location.href的几种用法
- myeclipse2017创建第一个servlet出现的问题
- Material Design之RecyclerView基本讲解与瀑布流的实现
- pandas 计算指标/哑变量
- opencv-python(13):Canny边缘检测
- JAVA_文件上传(html,js,后台逻辑)
- c++关于const和const*
- 22
- 大数据-极值数据学院
- sdnu1037约数之和
- 飞速(FS)数据中心级交换机为什么受欢迎?
- BigDecimal
- tomcat启动报错too low setting for -Xss stackoverflow
- Mac OS 在终端未启动的情况下,快速调出终端