图片上传到本地服务器

JSP

 <ul>
                                        <li style="width: 30%;height: 66px;margin-top: 70px;margin-left: 109px;">
                                            <a class="mini-button" href="javascript:;" id="btnUpLoad" style="margin: 31px 15px;float: right;width: 12%;height: 18px;">上传</a>
                                            <input type="file" id="upload" name="upload" style="width: 250px;" hidden/>
                                            <input class="mini-hidden" style="display: none" name="picture" id="picture" />
                                            <img src="" id="personnalPicView" style="width: 54%; height: 106px;margin: -40px 10px;" onerror="javascript:this.src='${contextPath}/resources/image/default-head-image.png';">
                                            <li style="width:60%;margin-left:62%; color: red;">
                                                <span id="">注:图片为.jpg, .jpeg, .png 格式!</span>
                                            </li>
                                        </li>
                                    </ul>

JS

  uploadFile({
            subfix: ['jpg', 'jpeg', 'png', 'bmp'],
            url: "/examination/ajax-upload.html",
            subfixTip: "请选择图片!",
            upload: 'upload',
            successCall: function(data) {
                mini.get("picture").setValue(data.savePath);
                $('#personnalPicView').attr('src', contextPath + data.savePath);
                mini.alert(data.flag);
            }
        });

ACTION

@Action("/examination/ajax-upload")
	public void uploadFile() {
		final Map<String, Object> result = Maps.newHashMap();
		// 判断文件大小
		if (this.upload.length() > (5 * 1000 * 1000)) {
			result.put("flag", "fail");
			result.put("msg", "上传文件大小超过限制, ,当前允许的最大文件大小为:5MB");
		} else {
			final String realTempPath = ServletActionContext.getServletContext().getRealPath(BASE_ATTACH_DIR);
			final String fileFix = this.uploadFileName.substring(this.uploadFileName.lastIndexOf("."));
			final String fileName = this.uploadFileName.substring(0, this.uploadFileName.lastIndexOf("."));
			final String savePath;
			if (StringUtils.isNotBlank(this.dir)) {
				savePath = FILE_SEPARATOR + this.dir + FILE_SEPARATOR + fileName + "_" + new Date().getTime() + fileFix;
				final File saveDirFile = new File(realTempPath + FILE_SEPARATOR + this.dir);
				if (!saveDirFile.exists()) {
					saveDirFile.mkdirs();
				}
			} else {
				savePath = FILE_SEPARATOR + new Date().getTime() + fileFix;
			}
			try {
				FileUtils.copyFile(this.upload, new File(realTempPath + savePath));
				result.put("flag", "上传成功!");
				result.put("savePath", BASE_ATTACH_DIR + savePath);
				result.put("fileName", this.getUploadFileName());
			} catch (final IOException e) {
				e.printStackTrace();
				result.put("flag", "fail");
				result.put("msg", "程序有误,联系管理员");
			}
		}

		sendResponseMsg(new Gson().toJson(result));
	}

存库

final String realTempPath = ServletActionContext.getServletContext().getRealPath("/");
			final String realTempPat1 = ServletActionContext.getServletContext().getRealPath(BASE_ATTACH_DIR);
			final String fileFix = doctorInfoFormVo.getPicture()
					.substring(doctorInfoFormVo.getPicture().lastIndexOf("."));
			File oldFile = new File(realTempPath + doctorInfoFormVo.getPicture());
			File newFile = new File(realTempPat1 + File.separator + doctorInfoFormVo.getZjhm() + fileFix);
//查到文件有相同的名字的文件删除掉
			if(newFile.exists()){
				newFile.delete();
			};
//修改已经上传的文件名字
			if (oldFile.renameTo(newFile)) {
				System.out.println("修改成功!");
			} else {
				System.out.println("修改失败");
			}
			String picurl=(BASE_ATTACH_DIR+FILE_SEPARATOR+doctorInfoFormVo.getZjhm() + fileFix);
			doctorInfoFormVo.setPicture(picurl);
			this.straightService.saveOrUpdate(doctorInfoFormVo);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值