版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39220472/article/details/82556288
项目:maven+ssm(spring+springmvc+mybatis)
编译工具:eclipse
1.添加上传依赖包:
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
2.在springmvc.xml配置文件加上上传配置信息:
<!-- 配置文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
3.配置tomcat虚拟图片服务器:
在eclipse的Servers双击tomcat
点击Modules显示界面:
点击Add Web Module,添加虚拟服务器
注:/img是虚拟服务器路径,E:\picture是我上传图片在电脑的 路径
点击ok:显示我们配置的图片服务器信息
4.运行项目,上传图片并显示图片。
上传成功并显示图片:
图片上传在电脑E盘:
5.编码详情:
在前端页面显示tomcat虚拟图片服务 里面的图片是:
src="/img/${person.a_img }"也就是src="配置虚拟图片服务器路径/+你存放进数据库的图片名字"
<c:if test="${person.a_img !=null }">
<tr>
<td>头像</td>
<td class="c7"><img class="img" style="width:200px;height:100px;"
src="/img/${person.a_img }" /></td>
</tr>
</c:if>
页面图片上传form表单:
注:上传操作form表单一定要加enctype="multipart/form-data"
<!-- form表单 -->
<div id="dlg" class="easyui-dialog" title="修改个人信息" data-options="iconCls:'icon-save',closed:true,modal:true" style="display:none;width:400px;height:300px;padding:10px;top:30px">
<form enctype="multipart/form-data" action="<%=request.getContextPath()%>/admin/update" method="post">
<table>
<tr><td>账号</td><td><input name="a_account" readonly="readonly" value="${person.a_account }"/><input name="a_id" type="hidden" readonly="readonly" value="${sessionScope.admin.a_id }"/></td></tr>
<tr><td>姓名</td><td><input name="a_name" value="${person.a_name }"/></td></tr>
<tr><td>性别</td><td>
<input type="radio" id="a_sex" name="a_sex" checked="true" value="0"> <label for="flat-radio-1" class="">男</label>
<input type="radio" id="a_sex" name="a_sex" value="1"> <label for="flat-radio-1" class="">女</label>
</td></tr>
<tr><td>联系电话</td><td><input name="a_phone" value="${person.a_phone }"/></td></tr>
<tr>
<td>头像</td>
<td><input type="file" name="file" id="image_input"></td>
</tr>
<tr><td colspan="2"> <input type="submit" class="btnAll" value="修改"/> </td></tr>
</table>
</form>
</div>
springmvc上传图片controller层代码:
/**
* 修改个人信息
* @param record
* @param file 头像图片上传
* @return
*/
@RequestMapping("/update")
public String update(Admins record, MultipartFile file) {
String img_path=new FileUtils().getImgPath(file);
if(img_path!=null){
record.setA_img(img_path);
}
adminsService.updateByPrimaryKeySelective(record);
return "redirect:/admin/selectById?id=" + record.getA_id();
}
文件上传工具类:
package comit.util;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* 文件上传工具类
* @author admin
*
*/
public class FileUtils {
/**
*
* @param originalFilename
* @return
*/
public String getFileName(String originalFilename){
String fileName=String.valueOf(System.currentTimeMillis()) + "." + originalFilename;
return fileName;
}
/**
* 返回文件名
* @param file
* @return
*/
public String getImgPath(MultipartFile file) {
if("".equals(file.getOriginalFilename())){
return null;
}
//获取上传图片的文件名,变为时间+图片名
String fileName = getFileName(file.getOriginalFilename());
System.out.println("filename:" + fileName);
String filePath = Constant.PICTURE_PATH + fileName;
//创建文件对象
File tagetFile = new File(Constant.PICTURE_PATH + fileName);
//文件名不存在 则新建文件,并将文件复制到新建文件中
if (!tagetFile.exists()) {
try {
tagetFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
//保存图片
file.transferTo(tagetFile);
return fileName;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}