单文件上传

public String appinfoadd(AppInfo info,HttpServletRequest request,HttpSession session,
				@RequestParam(value ="a_logoPicPath", required = false) MultipartFile attach){
			System.err.println("=============>添加实体类:" + info.getSoftwareName());
			System.err.println("=============>文件上传:" + attach);
			
			//文件上传
			String idPicPath = null;
			String idTocPath = null;
			//判断文件是否为空
			if (!attach.isEmpty()) {
				// 定义上传的目标路径
				String path = request.getSession().getServletContext()
						.getRealPath("statics" + File.separator + "uploadfiles");
				String path = "D:\\xiangm\\AppInfoSystem\\WebContent\\statics\\uploadfiles";
				System.err.println("uploadFile path ============== > " + path);
				// 获取原文件名
				String oldFileName = attach.getOriginalFilename();
				System.err.println("uploadFile oldFileName ============== > " + oldFileName);
				// 获取原文件名的后缀
				String prefix = FilenameUtils.getExtension(oldFileName);// 原文件后缀
				System.err.println("uploadFile后缀 prefix============> " + prefix);
				int filesize = 500000;//设置文件大小
				System.err.println("uploadFile size============> " + attach.getSize());
				// 上传大小不得超过 500k
				if (attach.getSize() > filesize) {
					System.err.println("----------------文件过大");
					request.setAttribute("uploadFileError", " * 上传大小不得超过 500k");
					return "jsp/useradd";

				} else if (prefix.equalsIgnoreCase("jpg")
						|| prefix.equalsIgnoreCase("png")
						|| prefix.equalsIgnoreCase("jpeg")
						|| prefix.equalsIgnoreCase("pneg")) {// 上传图片格式不正确
					// 新的照片名称,毫秒数加随机数,确保不能重复
					System.out.println("-----------------进入");
					String fileName = System.currentTimeMillis()
							+ RandomUtils.nextInt(1000000) + "_Personal.jpg";
					System.out.println("-----------------ddd");
					System.err.println("new fileName======== " + attach.getName());
					// 创建文件对象,此文件对象用于接收用户上传的文件流
					File targetFile = new File(path, fileName);
					if (!targetFile.exists()) {
						targetFile.mkdirs();
					}
					// 保存
					try {
						// 把MultipartFile中的文件流数据的数据输出至目标文件中
						attach.transferTo(targetFile);
					} catch (Exception e) {
						e.printStackTrace();
						System.err.println("----------------上传图片失败");
						request.setAttribute("uploadFileError", " * 上传失败!");
						return "jsp/useradd";
					}
					idTocPath = request.getContextPath()+"/statics/uploadfiles/"+fileName;
					// 获取文件的的名称保存到数据库中
					idPicPath = path + File.separator + fileName;
				} else {
					System.err.println("----------------上传图片格式不正确");
					request.setAttribute("uploadFileError", " * 上传图片格式不正确");
					return "jsp/useradd";
				}
			}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值