SpringBoot图片上传(二)

需求简介:做新增的时候,需要上传图片。(⊙o⊙)…这需求描述也太简单了吧,限制文件大小60*60 512kb ,第一次做,记录一下嗷,废话就不啰嗦了 上代码

代码:

 

//html代码
<div class="col-md-5">
<input type="file" value="上传图标" id="fileupload" th:onchange="|fileUpload()|" style="display:none"/>
  //此src是 上传的小图标图片
<img src="D:/img/shangchuan.png" alt="" th:onclick="|doUpload()|" id="imageId" style="margin-top: 10px;"/>
<input type="hidden" id="serviceIcon" name="serviceIcon" class="form-control"/>
</div>
//js代码 <script type="text/javascript" th:inline="javascript" xmlns:th="http://www.w3.org/1999/xhtml">
function doUpload(){
$("#fileupload").trigger("click");
}
   //文件上传
function fileUpload(file){
debugger;
var myform = new FormData();
myform.append('file', $("#fileupload")[0].files[0]);
var max_size = 512;// 300k
var tmpFile = $("#fileupload").val();
var imgtype = tmpFile.toLowerCase().split('.');
if ((imgtype[1]) != "jpg") {
layer.msg("图片只支持jpg格式");
tmpFile.value = "";
return false;
} else {
$.ajax({
type:"POST",
url: rootPath + "/serv/service/updatefile",
data:myform,
cache: false,
contentType: false,
processData: false,
dataType: "json",//返回的数据格式
async: false,
success: function (data) {
debugger;
if(data==="1" ){
layer.msg("上传失败,图片内存过大");
}else if(data=="2"){
layer.msg("上传失败,图片内存过大");
}else{
var path="/oh-manager/"+data;
                 //取代原来的上传图片的小图标 展示上传后的图片
$("#imageId").attr("src",path);
$("#serviceIcon").val(path);
}
},
error: function (jqXHR, textStatus, errorThrown) {

}
});
}
     return false;
}
</script>

//后台java代码: @RequestMapping(value="/updatefile")
@ResponseBody
public String updatefile(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
String contentType = file.getContentType();
String fileName = file.getOriginalFilename();
   try {
File targetFile = new File("D:/");
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream("D:/temp/"+fileName);
out.write(file.getBytes());
File fileRoute = new File("D:/temp/"+fileName);
BufferedImage sourceImg =ImageIO.read(new FileInputStream(fileRoute));
Long size = file.getSize() / 1024; // 图片大小
int wid=sourceImg.getWidth(); // 源图宽度
int hei=sourceImg.getHeight(); // 源图高度
if(size>512){
fileName="1";
}
if(wid>60 || hei>60){
fileName="2";
}
out.flush();
out.close();
} catch (Exception e) {
// TODO: handle exception
}
long length=file.getSize();
//返回json
return fileName; //fileName就是上边ajax的data
} //配置文件constant.properties配置文件路径 web.upload-path=D:/temp/
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,\
classpath:/static/,classpath:/public/,file:${web.upload-path}
posted @   ジ绯色月下ぎ  阅读(937)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示