JAVA 图片上传

@ResponseBody
	@RequestMapping("/uploadImg")
	public HashMap<String, Object> uploadPicture(
			@RequestParam(value = "file", required = false)MultipartFile file,
			@RequestParam(value = "savePath", required = false)String savePath,
			HttpServletRequest request) {
		File targetFile = null;
		File targetFile2 = null;
		long size = file.getSize();
		String url = "";// 返回存储路径
		String url2 = "";
		String realurl = "";
		String tifYslj= "";
		HashMap<String, Object> map = new HashMap<String, Object>();
		String fileName = file.getOriginalFilename();// 获取文件名加后缀
		String fileNameTmp = file.getOriginalFilename();//
		map.put("fileName", fileName);
		String wjmc = fileNameTmp.split("\\.")[0];
		if (fileName != null && fileName != "") {
			String returnUrl = request.getSession().getServletContext()
					.getRealPath("/upload/"+savePath) ;// 存储路径
			String fileF = fileName.substring(fileName.lastIndexOf("."),
					fileName.length());// 文件后缀
			// 先判断文件是否存在
			File file1 = new File(request.getSession().getServletContext()
					.getRealPath("/upload/"+savePath));
			// 如果文件夹不存在则创建
			if (!file1.exists() && !file1.isDirectory()) {
				file1.mkdirs();
			}
			 File file2 =new File(request.getSession().getServletContext()
						.getRealPath("/upload2/"+savePath)); 
	            if(!file2 .exists()  && !file2 .isDirectory()){      
	                file2 .mkdirs();  
	            }
			if (".tif".equals(fileF) || ".TIF".endsWith(fileF)) {
				fileName = +new Random().nextInt(10000)
						+ System.currentTimeMillis() + fileF;
				 targetFile2 = new File(file2, fileName);
				try {
					 file.transferTo(targetFile2);
                     url2 =targetFile2.toString().replaceAll("\\\\", "/");
                     File f = new File(url2);
                     FileInputStream inputStream = new FileInputStream(f);
                     file = new MockMultipartFile(f.getName(), inputStream);
                     tifYslj = "/upload2/"+savePath + "/"+fileName;
                	 fileF = ".jpg";
                	 fileName=+new Random().nextInt(10000)+System.currentTimeMillis()+fileF;//新的文件名
                     targetFile = new File(file1, fileName);
                     url =targetFile.toString().replaceAll("\\\\", "/");
                     // tif转换到jpg格式 
                     String output2 =Global.getUserfilesBaseDir()+fileName;
//                 	 String output2 = "d:/image/"+fileName;
                     RenderedOp src2 = JAI.create("fileload", url2);
                     OutputStream os2 = new FileOutputStream(output2);
                     JPEGEncodeParam param2 = new JPEGEncodeParam();
                     //指定格式类型,jpg 属于 JPEG 类型
                     ImageEncoder enc2 = ImageCodec.createImageEncoder("JPEG", os2, param2);
                     enc2.encode(src2);
                     os2.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				fileF = ".jpg";
				fileName = +new Random().nextInt(10000)
						+ System.currentTimeMillis() + fileF;// 新的文件名
				targetFile = new File(file1, fileName);
				url = targetFile.toString().replaceAll("\\\\", "/");
				try {
					/* tif转换到jpg格式 */
					String output2 = request.getSession().getServletContext()
							.getRealPath("/upload/"+savePath) + fileName;
					RenderedOp src2 = JAI.create("fileload", url2);
					OutputStream os2 = new FileOutputStream(output2);
					JPEGEncodeParam param2 = new JPEGEncodeParam();
					// 指定格式类型,jpg 属于 JPEG 类型
					ImageEncoder enc2 = ImageCodec.createImageEncoder("JPEG",
							os2, param2);
					enc2.encode(src2);
					os2.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				fileName = +new Random().nextInt(10000)
						+ System.currentTimeMillis() + fileF;// 新的文件名

				targetFile = new File(file1, fileName);

				try {
					file.transferTo(targetFile);
					realurl = returnUrl + fileName;
					url = targetFile.toString().replaceAll("\\\\", "/");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		String wjlj="/upload/"+savePath+"/"+fileName;
		
		map.put("url", url);
		if (!url2.equals("")) {
		  
			map.put("url2", tifYslj);
		} else {
			map.put("url2", wjlj);
		}
		map.put("wjmc", wjmc);
		map.put("fileName", fileNameTmp);
		map.put("size", size);
		map.put("wjlj",wjlj);
		map.put("realurl", realurl);
		return map;
	}

传入file和savepath即可。map中返回多个数据

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值