app对接的项目中图片上传的方式

第一种方式:是app传过来的图片



public Object register(Users us,
			@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request) {
			String[] str = file.getContentType().split("/");
			String path = request.getSession().getServletContext()
					.getRealPath("portrait");
			String fileName = new Date().getTime() + "." + str[1];
			File targetFile = new File(path, fileName);
			if (!targetFile.exists()) {
				targetFile.mkdirs();
			}
			try {
				file.transferTo(targetFile);// 保存文件
			} catch (Exception e) {
				e.printStackTrace();
			}
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ request.getContextPath() + "/";
		us.setUsimg(basePath + "portrait/" + fileName);
	}



第二种后台发布的图片


	public String update(
			@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request) {
		if (!file.isEmpty()) {
			String path = request.getSession().getServletContext()
					.getRealPath("carphoto");
			String[] str = file.getContentType().split("/");

			String fileName = new Date().getTime() + "." + str[1];
			File targetFile = new File(path, fileName);
			if (!targetFile.exists()) {
				targetFile.mkdirs();
			}
			try {
				InetAddress addr = InetAddress.getLocalHost();
				file.transferTo(targetFile);// 保存文件

				String basePath = request.getScheme() + "://"
						+ addr.getHostAddress().toString() + ":"
						+ request.getServerPort() + request.getContextPath()
						+ "/";
				String img2 = phh.getPhimg();
				String path1 = request.getServletContext().getRealPath("")
						+ img2.substring(img2.indexOf("xxxxx/") + 5);
				File fif = new File(path1);
				if (fif.exists()) {
					fif.delete();
				}
				phh.setPh_img(basePath + "carphoto/" + fileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
两种方式残生的图片地址   
http://192.168.0.119:8080/XXXXX/portrait/1488962605223.png

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值