<!--相关的几个模态框-->
<!--1 S 上传图片的模态框-->
<%--隐藏打开的index--%>
<input type="hidden" id="hidden_picture_index">
<div class="x-body" style="display: none" id="pictureModal">
<div class="layui-upload">
<!--隐藏培养方案编号-->
<input type="hidden" id="hidden_trainSchemeId">
<!--隐藏上传文件的操作-->
<input type="hidden" id="hidden_tmp_index">
<button type="button" class="layui-btn" id="structurePicture"><!--动态赋值--></button>
<!--预留一个预览的img标签-->
<div id="imgDiv" style="display: none;margin-top: 10px;">
<img id="imgPreview" width="400px" height="200px">
</div>
<div id="promptDiv" style="margin-top: 10px;display: none">
<p><font style="font-size: 30px">您还没有上传课程关系结构图,请先上传课程关系结构图!</font></p>
</div>
</div>
</div>
<!--1 E 上传图片的模态框-->
2.上传文件的JS
layui.use(['upload','layer'],function () {
var upload = layui.upload,layer = layui.layer;
//普通图片上传
var uploadInst = upload.render({
elem: '#pic' //绑定元素
, size: 500 //限制文件大小,单位 KB
, accept: 'file' //普通文件
, url: layui.setter.apiRoot + api.upload //上传接口
, before: function (obj) { //obj参数包含的信息,跟 choose回调完全一致,可参
})
this.data={savename :$("#temporarypic").html()};
layer.load(); //上传loading
}
, done: function (res, index, upload) {
layer.closeAll('loading'); //关闭loading
if (res.code == "SUCCESS") {
$('#picurl').val(res.data);
$('#temporarypic').attr('src', urltemporary + res.data);
} else {
layer.alert('上传失败');
}
}
, error: function (index, upload) {
layer.closeAll('loading'); //关闭loading
}
});
controller
@RequestMapping(value = "upload", produces = "application/json;charset=utf-8")
public ResponseModel upload(MultipartFile file,String savename) throws Exception {
AccountModel accountModel = currentAccount();
ResponseModel responseModel = new ResponseModel();
String filename = carouselService.upload(file, temporaryfilePath, formalfilePath,accountModel.getCompanycode());
return responseModel.setData(filename);//返回名称
}
service
public String upload(MultipartFile file, String temporaryfilePath, String formalfilePath, String companycode ) throws Exception {
String fileName = UploadFile.getupload(file, temporaryfilePath, formalfilePath);
return fileName;
}
util
public static String getupload(org.springframework.web.multipart.MultipartFile file, String filePath, String formalfilePath) throws Exception {
//a.jpg--->.jpg
String fileName = UUID.randomUUID().toString() + ertName;//uuid创建文件
try {
File dir = new File(filePath);
File dirformal = new File(formalfilePath);
if (!dir.exists()) {
//创建文件夹
if (!dir.mkdirs()) {
return "";
}
}
if (!dirformal.exists()) {
//创建文件夹
if (!dirformal.mkdirs()) {
return "";
}
}
FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(new File(filePath + fileName)));//把文件写入磁盘
} catch (IOException e) {
e.printStackTrace();
}
return fileName;//返回名称
}
附带文件移动的方法:
/**
* 文件 移动
*
* @param tempFile:移动对象
* @param descFile:移动之后的对象
* @return 文件是否移动成功
*/
public static boolean renameTo(String tempFile, String descFile) {
Boolean isCopyFile = true;
try {
//文件路径model(比如:apk的路径,ipa的路径)
if (!tempFile.equalsIgnoreCase("") && !descFile.equalsIgnoreCase("")) {
File dest = new File(descFile);
File file = new File(tempFile);
//判断移动的两个路径是否是一样的
if (dest.getPath().equals(file.getPath())) {
return true;
}
//如果这个文件已存在的话就删除(desc)copy的位置,并且显示
if (dest.exists()) {
return true;
// dest.delete();
}
//copy文件 file 复制到dest
return file.renameTo(dest);
}
} catch (Exception ex) {
isCopyFile = false;
ex.printStackTrace();
}
return isCopyFile;
}