<form action="${path}/upload/uploadPic.do" method="post" enctype="multipart/form-data">
<div>

<input type="file" id="input-image" name="input-image">
<input id="input-relative-path" name="imgs" type="hidden" >
<input id="input-last-path" type="hidden">
<input type="submit" value="上传图片">
</div>
</form>
@Controller
@RequestMapping("/upload")
public class UploadController extends BaseController {
@RequestMapping(value = "/uploadPic", method = RequestMethod.POST)
@LoginCheck
public void uploadPic(HttpServletRequest request, PrintWriter out, String lastRealPath) throws IOException {
// 将当前上下文初始化给CommonsMultipartResolver
CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
// 检查form中是否有enctype="multipart/form-data"
if (resolver.isMultipart(request)) {
// 强制转化request
MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
// 从表单获取input名称
Iterator<String> iterable = req.getFileNames();
// 存在文件
if (iterable.hasNext()) {
String inputName = iterable.next();
// 获得文件
MultipartFile mf = req.getFile(inputName);
byte[] mfs = mf.getBytes();
// 定义文件名
String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
Random random = new Random();
for (int i = 0; i < 3; i++) {
fileName = fileName + random.nextInt(10);
}
// 获得后缀名
String oriFileName = mf.getOriginalFilename();
String suffix = oriFileName.substring(oriFileName.lastIndexOf("."));
// 上传图片到本地
String localPath = "/Users/ZR/Desktop/webPro/console/src/main/webapp/image/" + fileName + suffix;
mf.transferTo(new File(localPath));
// 获取图片的宽高
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(new File(localPath)));
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
// 获取文件大小
long size = mf.getSize();
}
}
}
}
<!-- 文件上传的视图解析器,id值是固定的 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="1024000"/> <!-- 其他属性 --> </bean>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有