参考: http://www.layui.com/doc/modules/upload.html
<1> 文件上传(以下函数必须要在js文件加载时执行)
upload.render({
elem: '#id',
url: '/api/upload/',
before: function(obj){ //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
layer.load(); //上传loading
},
done: function(res, index, upload){
layer.closeAll('loading'); //关闭loading
},
error: function(index, upload){
layer.closeAll('loading'); //关闭loading
}
});
<2> 文件下载 参考:https://memorynotfound.com/spring-mvc-download-file-examples/
package com.memorynotfound.controller;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@Controller
@RequestMapping("/download")
public class DownloadController {
private static final String FILE\_PATH = "/tmp/example.pdf";
private static final String APPLICATION\_PDF = "application/pdf";
@RequestMapping(value = "/a", method = RequestMethod.GET, produces = APPLICATION\_PDF)
public @ResponseBody void downloadA(HttpServletResponse response) throws IOException {
File file = getFile();
InputStream in = new FileInputStream(file);
response.setContentType(APPLICATION\_PDF);
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
response.setHeader("Content-Length", String.valueOf(file.length()));
FileCopyUtils.copy(in, response.getOutputStream());
}
@RequestMapping(value = "/b", method = RequestMethod.GET, produces = APPLICATION\_PDF)
public @ResponseBody HttpEntity<byte\[\]> downloadB() throws IOException {
File file = getFile();
byte\[\] document = FileCopyUtils.copyToByteArray(file);
HttpHeaders header = new HttpHeaders();
header.setContentType(new MediaType("application", "pdf"));
header.set("Content-Disposition", "inline; filename=" + file.getName());
header.setContentLength(document.length);
return new HttpEntity<byte\[\]>(document, header);
}
@RequestMapping(value = "/c", method = RequestMethod.GET, produces = APPLICATION\_PDF)
public @ResponseBody Resource downloadC(HttpServletResponse response) throws FileNotFoundException {
File file = getFile();
response.setContentType(APPLICATION\_PDF);
response.setHeader("Content-Disposition", "inline; filename=" + file.getName());
response.setHeader("Content-Length", String.valueOf(file.length()));
return new FileSystemResource(file);
}
private File getFile() throws FileNotFoundException {
File file = new File(FILE\_PATH);
if (!file.exists()){
throw new FileNotFoundException("file with path: " + FILE\_PATH + " was not found.");
}
return file;
}
出现的问题:
1. 上传后,出现文件下载框(一般为ie下),那么你需要在服务端对response的header设置 Content-Type: text/html
response.addHeader("Content-Type","text/html");
2. 如果上传后,文件名称回显为乱码
response.addHeader("Content-Type","text/html;charset=utf-8");
手机扫一扫
移动阅读更方便
你可能感兴趣的文章