*1.bootstrap-fileinput 插件git下载地址
https://github.com/kartik-v/bootstrap-fileinput.git
2.解决使用bootstrap-fileinput得到返回值
1
2
3
4
5
6
7
8
9
10
11
12
|
$(
"#file-0a"
).fileinput({
allowedFileExtensions : [
'jpg'
'png'
'gif'
],
//allowedFileTypes: ['image','video','flash'],
slugCallback :
function
(filename) {
return
filename.replace(
'('
'_'
).replace(
']'
);
}
});
|
'#file-0a'
).on(
'fileuploaded'
(event,data,previewId,index) {
var
form = data.form,files = data.files,extra = data.extra,
response = data.response,reader = data.reader;
console.log(response);
//打印出返回的json
console.log(response.paths);
//打印出路径
2
"file-0a"
class=
"file"
type=
multiple
其中data-min-file-count=”1”是指文件上传最低数量
3.服务端代码
Bean
import java.util.List;
public class Picture {
private List<String> paths;
public List<String> getPaths()
{
paths;
}
public void setPaths(List<String> paths)
{
this
.paths = paths;
}
}
Controller
@ResponseBody
@RequestMapping(value=
"upload_img"
public Picture uploadImage(@RequestParam multipartfile[] upload_logo) throws IOException{
Picture pic =
new
Picture();
List<String> paths =
ArrayList<String>();
String dir = UploadUtil.getFolder();
for
(multipartfile myfile : upload_logo){
if
(myfile.isEmpty()){
}
else
{
"文件长度: "
+ myfile.getSize());
"文件类型: "
+ myfile.getContentType());
"========================================"
);
String path = UploadUtil.writeFile(myfile.getoriginalFilename(),dir,myfile.getInputStream());
"文件路径:"
+path);
paths.add(path);
}
}
pic.setPaths(paths);
pic;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
private UploadUtil() {
}
private static SimpleDateFormat fullSdf =
SimpleDateFormat(
"yyyyMMddHHmmssSSS"
private static SimpleDateFormat folder =
SimpleDateFormat(
/**
*
* @return
*/
public static String getFolder() {
folder.format(
Date());
}
/**
*
* @param srcName
* 原文件名
* @param dirName
* 目录名
* @param input
* 要保存的输入流
* @return 返回要保存到数据库中的路径
*/
public static String writeFile(String srcName,String dirName,InputStream input) throws IOException {
log.info(srcName);
// 取出上传的目录,此目录是tomcat的server.xml中配置的虚拟目录
// 取出虚拟目录的访问路径
String virtualDir = ContextUtil.getSysProp(
"virtual_dir"
//设置你虚拟目录访问路径
(
null
!= srcName) {
srcName = srcName.substring(srcName.indexOf(
"."
));
}
{
srcName =
".jpg"
;
}
String filename =
""
;
filename = uploadDir + File.separator + dirName + File.separator + fullSdf.format(
Date()) + srcName;
// 得到将要保存到数据中的路径
String savePath = filename.replace(uploadDir,monospace!important; min-height:auto!important'>);
savePath = virtualDir + savePath.replace(
"\\"
"/"
);
File file =
File(filename);
(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream fos =
FileOutputStream(file);
// 一次30kb
byte[] readBuff =
byte[1024 * 30];
int count = -1;
while
((count = input.read(readBuff,readBuff.length)) != -1) {
fos.write(readBuff,count);
}
fos.flush();
fos.close();
input.close();
savePath;