前端js
需要导入jquery.js 下载
<script type="text/javascript">
$("#testListAction").click(function () {
var form = new FormData();
for(let i in file){
form.append("file",file[i]);//表单文件流
}
$.ajax({
url: '<%=path%>/biz/UploadImage_mUploadImage.action' //上传接口
,type: "POST"
,data: form
,contentType: false
,processData: false
,success: function (data) {
console.log(data)
}
,error: function () {
alert(0)
}
})
})
</script>
Action中的代码
protected static final String MAIN_JSP = "/WEB-INF/page/main.jsp";
protected static final String UP_IMAGE_JSP = "/WEB-INF/page/Upload_Image.jsp";
private JSONArray data;
private UploadImage uploadImage = new UploadImage();
private HttpServletRequest request = ServletActionContext.getRequest();
//请求过来的路径
private String realURL = request.getSession().getServletContext().getRealPath("/");
//文件存储路径
private final String TEMP_URL = realURL;
public JSONArray getData() {
return data;
}
public void setData(JSONArray data) {
this.data = data;
}
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
private File[] file;//解析请求信息,获取表单传过来的File //获取FormData
private String[] fileContentType;//上传图片类型
public String[] getFileContentType() {
return fileContentType;
}
public void setFileContentType(String[] fileContentType) {
this.fileContentType = fileContentType;
}
private String[] fileFileName;//文件名
public String[] getFileFileName() {
return fileFileName;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}
//单链接多图片上传
public String uploadImage() throws Exception {
System.out.println("接收到前台发过来的信号");
JSONArray jsonArray = new JSONArray();
List<UploadImage> uploadImage = new ArrayList<UploadImage>();
if(file.length>0){//有文件输入
//调用图片上传的接口
uploadImage = uploadImageService.uploadImage(file, TEMP_URL, fileFileName);
}
setForwardView(MAIN_JSP);
//如果上传成功,则返回消息
if(!uploadImage.isEmpty()){
request.setAttribute("msg",1);
jsonArray = JSONArray.fromObject(uploadImage);
System.out.println("--获取到转换为json格式的内容:"+jsonArray.toString());
this.setData(jsonArray);
}
return SUCCESS;
}
Service类
Properties properties = new Properties();
protected final int SIZE = 10;//一次最多上传10个图片
//上传图片的Service方法
@Override
public List<UploadImage> uploadImage(File[] files, String TEMP_URL, String[] fileFileName) throws IOException {
//定义图片上传数组
List<UploadImage> uploadImages = new ArrayList<UploadImage>();
String[] uploadFileURL = new String[SIZE];
//获取配置文件的信息
properties = PropertiesLoaderUtils.loadAllProperties("conf//imagePath.properties");
//获取配置文件的imagesURL
String URL = properties.getProperty("imagesURL");
TEMP_URL.replaceAll("/","/\\");
File file = new File(TEMP_URL + URL);//文件存储绝对路径
if(!file.exists()){
file.mkdir();//如果文件不存在则创建文件
}else {
System.out.println("创建目录" + "失败,目标目录已经存在");
}
boolean flag = false;
for(int i=0;i<files.length;i++){
//图片文件名 为避免重名生成一个uuid作为文件名的前缀
String imageName = "image-" + UUID.randomUUID().toString().replace("-", "") + "_" + fileFileName[i];
//把图片拷贝到对应的文件夹
try {
FileUtils.copyFile(files[i], new File(file, imageName));
flag = true;
}catch (Exception e){
e.printStackTrace();
}
if(true==flag){//图片上传成功
//文件相对路径
uploadFileURL[i] = URL.replaceAll("\\\\", "") + "/" + imageName;
System.out.println("文件名:" + imageName);
//文件类型
String fileType = fileFileName[i];
//获取文件后缀名
fileType = fileType.substring(fileType.lastIndexOf(".") + 1);
System.out.println("文件类型:" + fileType);
//创建图片信息类并初始化参数
UploadImage uploadImage = new UploadImage(getImageSize(files[i].length()), fileType, imageName, TEMP_URL.replaceAll("\\\\", "/") + uploadFileURL[i], uploadFileURL[i]);
//存储图片文件信息到数组中
uploadImages.add(uploadImage);
}
}
return uploadImages;
}