java中ajax实现文件上传下载
来源:互联网 发布:得无异乎的异 编辑:程序博客网 时间:2024/06/05 07:31
上传前台html:注意type不要用submit
<form id="upload-form" method="post" enctype="multipart/form-data" >
<div class="modal-body"><input type="file" name="myFile"/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"> 取消</button>
<button type="button" onclick="updownload.upload()" class="btn btn-primary" >上传</button>
</div>
</form>
上传前台js:注意用formData定义文件流才可以使用ajax上传
updownload.upload=function(){
var formData = new FormData(document.getElementById("upload-form"));
$.ajax({
url: "updownload/upload",
method: 'POST',
data: formData,
contentType: false,
processData: false,
success: function (resp) {
if(resp.result==1){
$('#myModal').modal('hide');
comm.showSuccessToast("上传成功");//成功提示
}else{
comm.showFailToast("上传失败");//失败提示
}
updownload.searchList();
}
});
}
上传后台:
@RequestMapping(value ="/upload",method = RequestMethod.POST)
@ResponseBody
public RespJson upload(@RequestParam MultipartFile myFile, HttpSession session) throws IOException {
//获得原来文件名(含后缀名)
String originalFilename = myFile.getOriginalFilename();
int pos = originalFilename.lastIndexOf(".");
//原文件后缀名
String suffix = originalFilename.substring(pos);
//保存文件
//ServletContext application = session.getServletContext();
//String realPath = application.getRealPath("D:\\workspace111\\ycpolice-web\\web\\static\\updownload");
String realPath = "D:\\workspace111\\ycpolice-web\\web\\static\\updownload";
//产生一个uuid随机文件名
String uuid = UUID.randomUUID().toString();
String fullPath = realPath + File.separator + uuid + suffix;
InputStream in = null;
try {
in = myFile.getInputStream();
OutputStream out = new FileOutputStream(new File(fullPath));
int len = 0;
byte[] buf = new byte[3 * 1024];
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
//数据库插入数据
TUpdownloadEntity upload=new TUpdownloadEntity();
upload.setFileName(uuid+suffix);
updownloadService.save(upload);
return RespJsonFactory.buildSuccess();
}
下载前台:
只需要传你要下载的文件名就可以了
下载后台:
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void downLoad(HttpServletRequest request, HttpServletResponse response,String fileName) {
//String realPath = request.getSession().getServletContext().getRealPath("D:\\workspace111\\ycpolice-web\\web\\static\\updownload");
String realPath = "D:\\workspace111\\ycpolice-web\\web\\static\\updownload";
//获得服务器端某个文件的完整路径
String fullPath = realPath + File.separator + fileName;
//设置响应
response.setContentType("application/force-download");
//设置响应头信息
response.setHeader("Content-Disposition", "attachment;fileName="+fileName);// 设置文件名;
//文件名有中文时设置编码
//response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("GBK"),"ISO-8859-1"));
try {
File downloadFile = new File(fullPath);
FileInputStream inputStream = new FileInputStream(downloadFile);
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
}
}
- java中ajax实现文件上传下载
- JAVA中使用FTPClient实现文件上传下载
- JAVA中使用FTPClient实现文件上传下载
- JAVA中使用FTPClient实现文件上传下载
- JAVA中使用FTPClient实现文件上传下载
- JAVA中使用FTPClient实现文件上传下载
- JAVA中使用FTPClient实现文件上传下载
- java实现文件上传下载
- Java实现文件上传下载
- java中上传下载文件
- ajax 实现文件上传下载的方法
- WebService中实现 上传下载文件
- WebService中实现 上传下载文件
- java实现ftp文件上传下载
- Java实现FTP上传下载文件
- java实现文件的上传下载
- java实现文件的上传下载
- Java实现文件上传下载功能
- megaface使用
- 线程安全性问题
- android studio 重写类的方法
- C 运算符
- iOS 多线程的四种技术方案 OC
- java中ajax实现文件上传下载
- IL语言如何在.NET下运行?
- Bootstrap中的轮播
- Java虚拟机:Java垃圾回收(GC)机制详解
- SSL证书对电商平台的重要性
- web项目发布到tomcat中编译后没有classes文件的原因
- BZOJ2097: [Usaco2010 Dec]Exercise 奶牛健美操
- 简单python爬虫淘宝图片+界面编程+打包成exe
- 如何将同一 VNET 下的虚拟机从经典部署模型迁移到 Azure Resource Manager