java项目实例图片上传

本文介绍了使用Spring的MultipartHttpServletRequest来实现文件上传的过程。首先,表单提交需设置为POST方式并添加enctype属性。然后,通过MultipartResolver转换请求,获取MultipartFile对象以读取文件流和文件格式。在jsp页面中设置文件输入字段,在Java后台处理文件上传逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                         使用 Spring中MultipartHttpServletRequest实现文件上传

一、上传对表单的要求

1) 表单的提交方式 :"method=post"

2) 表单的类型 :enctype="multipart/form-data"

3) 上传表单的文件项:<input type="file" name="xx" /> //其中名字是必须要的


二.上传的一般步骤

1.将request转化为MultipartHttpServletRequest multipartRequest= (MultipartHttpServletRequest)(request);

原理是:使用spring的CommosMultipartResolver 配置MultipartResolver 用于文件上传,DispatcherServlet 将调用 MultipartResolver 的 isMultipart(request) 方法检查当前 Web 请求是否为 multipart类型。如果是,DispatcherServlet 将调用 MultipartResolver 的 resolveMultipart(request) 方法,对原始 request 进行装饰,并返回一个 MultipartHttpServletRequest 供后继处理流程使用(最初的 HttpServletRequest 被偷梁换柱成了 MultipartHttpServletRequest),否则,直接返回最初的 HttpServletRequest。

获取文件 

         MultipartHttpServletRequest multipartRequest  =  (MultipartHttpServletRequest) request; //将request转化为MultipartHttpServletRequest

 MultipartFile multipartFile = multipartRequest.getFile("upload");//获取文件

         InputStream  is = multipartFile.getInputStream();//获取图片的文件流
        String uploadContentType = multipartFile.getOriginalFilename();//获取文件的格式

2.判断文件的格式(必须为.jpg/.gif/.bmp/.png文件))

3.对图片进行处理(压缩图片),并输出到指定目录

4.在数据库中保存图片的路径名imgDir+imgName

jsp页面代码

<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/include.jsp"%>
<%@page import="java.util.Date"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link href="<ph:root/>/s/css/style.css" type="text/css" rel="stylesheet" />
<link href="<ph:root/>/s/css/gh-buttons.css" type="text/css"
	rel="stylesheet" />
<link href="<ph:root/>/s/css/selectinput.css" type="text/css"
	rel="stylesheet" />
<link href="<ph:root/>/s/js/ueditor/themes/default/css/ueditor.css"
	type="text/css" rel="stylesheet" />
<script type="text/javascript" src="<ph:root/>/s/js/jquery.min.js"></script>
<script type="text/javascript" src="<ph:root/>/s/js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="<ph:root/>/s/js/jquery.form.js"></script>
<script type="text/javascript" src="<ph:root/>/s/js/common.js"></script>
<script type="text/javascript" src="<ph:root/>/s/js/checkForm.js"></script>

<script type="text/javascript">
	$(document).ready(function() {
		//$("textarea[name='content']").ckeditor();
		var uEditor = UE.getEditor("content1", {
			initialFrameWidth : 800,
			initialFrameHeight : 400,
			autoHeightEnabled : false,
			contextMenu : []
		});
		/* uEditor.onblur = function(){
			if(uEditor.getContent()){
				uEditor.commands.autotypeset.execCommand();
			}
		} */

	});

	function tolist() {
		window.location.href = "<ph:root/>/m/periodicals/list";
	}

	function submitForm() {
		if (!checkForm("form1")) {
			return false;
		}
		if (confirm("确认保存?")) {
			openLoad("正在处理,请稍后。。。");
			//防止重复提交
			$("#submit_btn").attr("disabled", true);
			$("#form1").ajaxSubmit({
				url : '<ph:root/>/m/periodicals/addContentDo',
				type : 'post',
				dataType : 'json',
				success : function(result) {
					closeLoad();
					$("#submit_btn").attr("disabled", false);
					if (result.resultCode == 0) {
						alert("提交成功");
						tolist();
						return true;
					} else {
						alert(result.resultMsg);
						return false;
					}
				}
			});
		}
	}
	
	function showPreviewImg(){
		$("#imgTr").show();
	}
	
	//图片预览
	function setImagePreview(id,oldId,localImageId) {
	    var docObj=document.getElementById(id);
	    var imgObjPreview=document.getElementById(oldId);
	    var allowExtention = ".jpg,.bmp,.gif,.png";
	    $("#"+oldId).show();
	    var extention = docObj.value.substring(docObj.value.lastIndexOf(".") + 1).toLowerCase(); //获取当前上传文件的扩展名
	    if(allowExtention.indexOf(extention) == -1){
	    	alert("请选择.jpg,.bmp,.gif,.png文件");
	    	return false;
	    } 


	    if(docObj.files && docObj.files[0]){
	    	if (window.FileReader) {
	        	var reader = new FileReader();
	            reader.onload = function(e) {
	                document.getElementById(oldId).setAttribute("src", e.target.result);
	            }
	            reader.readAsDataURL(docObj.files[0]);
	        } else if (browserVersion.indexOf("SAFARI") > -1) {
	            alert("不支持Safari浏览器6.0以下版本的图片预览!");
	        } else {
	            alert("不支持您当前使用的浏览器的图片预览!");
	        }
	    }else{
	        //IE下,使用滤镜
	        docObj.select();
	        window.parent.document.body.focus();
	        var imgSrc = document.selection.createRange().text;
	        var localImagId = document.getElementById(localImageId);
	        //必须设置初始大小
	        localImagId.style.width = "100px";
	        localImagId.style.height = "100px";
	        //图片异常的捕捉,防止用户修改后缀来伪造图片
			try{
	            localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
	            localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
	        }catch(e){
	            alert("您上传的图片格式不正确,请重新选择!");
	            return false;
	        }
	       imgObjPreview.style.display = 'none';
	       //$("#"+oldId).hide();
	        document.selection.empty();
	        
	    }
	    return true;
	}
</script>
</head>
<body>
	<jsp:include page="../head.jsp"></jsp:include>
	<div id="content">
		<jsp:include page="../left.jsp"></jsp:include>
		<div class="right">
			<div class="titlebar">
				<span>当前功能:<a href="#"></a>期刊章节管理  >  <b>新增电子院报章节</b>
				</span>
				<div class="user-1"></div>
			</div>
			<div class="frame" id="frame">
				<div class="guntx_z">
					<div class="guntx_z1">
						<div id="bj3">
							<sf:form modelAttribute="periodicalsChapter" action="addDo"
								method="post" id="form1" name="form1"
								enctype="multipart/form-data">
								<input type="hidden" name="parentId" checkItems="N" value="${id}" />
								<%-- 1 图文新闻 --%>
								<div>
									<div class="titlebar1">
										<div class="tab_font1">
											<span style="margin-left: 30px; color: red">带 *
												号为必填项</span>
										</div>
									</div>

									<div class="titlebar2">
											<table width="97%" style="border:1px solid #ccc">
												<tr>
												  
													<td width="20%" align="right" bgcolor="#ffffff"><span
														class="fontts"> * </span>标题:</td>
													<td width="80%" align="left" bgcolor="#FFFFFF"
														style="padding-left: 20px;"><sf:input path="title"
															checkItems="N" style="width:300px;height:30px;"
															maxlength="50" placeholder="请输入期刊名" /></td>
												</tr>
												<tr>
							         <td align="right" bgcolor="#ffffff">封面图片:</td>
							       <td bgcolor="#FFFFFF" style="vertical-align: top;padding-left: 20px;">
								     <input id="upload" name="upload" type="file" style="width:367px;height:30px;" onchange="showPreviewImg();setImagePreview('upload','imgView','divNewPreview')"/> 
								   <span class="fonttsDef" id="thumbUrlRightId" style="text-align: right"> 
									尺寸:520*320 px 文件大小:100KB以内,文件格式:jpg,jpeg,gif,bmp,png 
								   </span>
							     </td>

						        </tr>
						          <%-- <c:if test="${upload!=null}"></c:if> --%>
						        <tr style="display:none" id="imgTr">
							    <td align="right" bgcolor="#ffffff">图片预览</td>
							      <td bgcolor="#FFFFFF" style="padding-left: 20px;">
								<img id="imgView" src=''  alt=""  style="display:none" width="150" height="100"/>
								<div id="divNewPreview"></div>
							     </td>
						         </tr>
												<tr>
													<td align="right" bgcolor="#ffffff"><span
														class="fontts"> * </span>内容:</td>
													<td align="left" bgcolor="#FFFFFF"
														style="padding-left: 20px;"><sf:textarea
															path="content" id="content1" /></td>
												</tr>
											</table>
										
									</div>
								</div>
								<div class="baocun button-group minor-group">
									<input type="button" onclick="return submitForm()"
										id="submit_btn" name="subButton" class="buttonqr" value="保存" />
									<input type="button" onclick="tolist()" name="subButton"
										class="buttonqr_cancel" value="取消" />
								</div>
							</sf:form>
						</div>
					</div>
				</div>
			</div>
			<!--发布内容代码结束-->

		</div>
	</div>
	<jsp:include page="../foot.jsp"></jsp:include>
	<script type="text/javascript" charset="utf-8"
		src="<ph:root/>/s/js/ueditor/ueditor.config.js"></script>
	<script type="text/javascript" charset="utf-8"
		src="<ph:root/>/s/js/ueditor/ueditor.all.js"></script>
	<script type="text/javascript" charset="utf-8"
		src="<ph:root/>/s/js/ueditor/lang/zh-cn/zh-cn.js"></script>
</body>
</html>


java 后台代码

controller

@Autowired
	private Executor threadPoolTaskExecutor;
/**
		    *后台添加电子院报章节内容
		    * 
		    * 
		    */
		@RequestMapping(value = "/m/periodicals/addContentDo")
		public void addContentDo(@ModelAttribute PeriodicalsChapter periodicalsChapter,Model model,HttpServletRequest request,HttpServletResponse response){
			Map<String,Object> map = new HashMap<String,Object>();
			InputStream is = null;
			Gson gson = new Gson();
			try{
				MultipartHttpServletRequest multipartRequest  =  (MultipartHttpServletRequest) request;  
				MultipartFile multipartFile = multipartRequest.getFile("upload");
				String uploadContentType = multipartFile.getOriginalFilename();
				if(multipartFile != null && multipartFile.getSize() != 0){
					String expandedName = CommonUtil.checkImg(uploadContentType);
					if(expandedName.equals("")){
						map.put("resultCode", 3);
						map.put("resultMsg", "文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)!");
						String jsonStr = gson.toJson(map);
						AjaxUtil.ajaxHtml(jsonStr, response);
						return ;
					}
					String imgName = System.currentTimeMillis() + "_" + ((int) (Math.random() * 900) + 100) + "." + expandedName;
					is = multipartFile.getInputStream();
					threadPoolTaskExecutor.execute(
							new ImageCompressionTask(is, imgName, 520, 320)
					);
					periodicalsChapter.setTopThumbUrl(CommonUtil.getFolder(imgName));
				}//压缩图片				
				periodicalsChapter.setCreateTime(new Date().getTime());//获取时间
				periodicalsChapter.setTitle(periodicalsChapter.getTitle());
				periodicalsChapter.setContent(periodicalsChapter.getContent());
				periodicalsChapter.setParentId(periodicalsChapter.getParentId());
				//保存
			if(periodicalsChapterService.addPeriodicalsChapter(periodicalsChapter)){					
				map.put("resultCode", 0);
				map.put("resultMsg", "发布成功");
				String jsonStr = gson.toJson(map);
				AjaxUtil.ajaxHtml(jsonStr, response);
				return ;
			}else{
				map.put("resultCode", 1);
				map.put("resultMsg", "发布失败");
				String jsonStr = gson.toJson(map);
				AjaxUtil.ajaxHtml(jsonStr, response);
			}
			}catch(Exception e){
				logger.error("保存异常",e);
				map.put("resultCode", 1);
				map.put("resultMsg", "发布失败");
				String jsonStr = gson.toJson(map);
				AjaxUtil.ajaxHtml(jsonStr, response);
			}
		} 


1. CommonUti类的 checkImg的方法,检查文件的格式
package com.rmyy.web.common;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.ui.ModelMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.rmyy.web.model.Channel;
import com.rmyy.web.util.DateUtil;
import com.rmyy.web.util.PropertyFileUtil;

public class CommonUtil {
	public static   String getFolder(String imgName) {
		// 图片上下文
		String imgContext = PropertyFileUtil.get("imgContext");
		String dateStr = DateUtil.getDate();
		String imgDir = imgContext + "/" + dateStr + "/";
		return imgDir+imgName;
	}
	
	public static String  checkImg(String uploadContentType){
		String expandedName = "";
		if (uploadContentType.endsWith(".jpg") || uploadContentType.endsWith(".JPG")) {
			// IE6上传jpg图片的headimageContentType是image/pjpeg,而IE9以及火狐上传的jpg图片是image/jpeg
			expandedName = "jpg";
		} else if (uploadContentType.endsWith(".png") || uploadContentType.endsWith(".PNG")) {
			// IE6上传的png图片的headimageContentType是"image/x-png"
			expandedName = "png";
		} else if (uploadContentType.endsWith(".gif")) {
			expandedName = "gif";
		} else if (uploadContentType.endsWith(".bmp")) {
			expandedName = "bmp";
		}
		return expandedName;
	}
	
	
	 // 校验文件尺寸跟大小
		public static String checkFileImg(MultipartFile file,String photoSizeStr,String photoWidthStr,String photoHeightStr) throws IOException {
			int photoSize = photoSizeStr!=null?Integer.parseInt(photoSizeStr):0;
        	int photoWidth = photoWidthStr!=null?Integer.parseInt(photoWidthStr):0;
        	int photoHeight = photoHeightStr!=null?Integer.parseInt(photoHeightStr):0;
			CommonsMultipartFile cf= (CommonsMultipartFile)file;
			String result = "";
			String tip = "请上传图片尺寸为"+photoWidth+"*"+photoHeight+"!";
			BufferedImage image =ImageIO.read(cf.getInputStream());
			//BufferedImage image = ImageIO.read(f);
			if (file.getSize() > photoSize * 1024) {
				result = "文件大小不得大于" + photoSize + "kb!";
				return result;
			}
			if (image.getWidth() < (photoWidth) || image.getWidth() > (photoWidth + 20)) {
				return tip;
			}
			
			if (image.getHeight() < (photoHeight) || image.getHeight() > (photoHeight + 20)) {
				return tip;
			}
			
			return null;
		}
		
	
}


2.图片压缩
package com.rmyy.web.common.task;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import com.rmyy.web.common.CommonUtil;
import com.rmyy.web.util.DateUtil;
import com.rmyy.web.util.ImageUtils;
import com.rmyy.web.util.PropertyFileUtil;

public class ImageCompressionTask implements Runnable{

	protected static Logger logger = Logger.getLogger(ImageCompressionTask.class);
	private InputStream is;
	private int width;
	private int height;
	private String fileName;
	
	/**
	 * 初始化参数
	 * @param file  图片
	 * @param id    ID
	 * @param type  图片分类1为商家管理图片2为产品分类图片
	 * @param fileName  图片名称
	 * @param width   高
	 * @param height  宽
	 */
	public ImageCompressionTask(InputStream is,String fileName,int width,int height){
		this.is = is;
		this.width = width;
		this.height = height;
		this.fileName = fileName;
	}
	
	
	public void run() {
		try{
			this.compressPic();
		}catch(Exception e){
			logger.error("生成压缩图失败! " ,e);
		} 
	}
	
	
	/**
	 * 图片压缩
	 * @return
	 */
	private String compressPic() throws Exception {
		String rootPath = PropertyFileUtil.get("imgPath");
		String path = getFolder(rootPath); //根路径下的子目录
		String urlPath =  path + fileName;
		
		BufferedImage buffImg;
		FileOutputStream output = null;
		try {
			String extentionName = "";
			if(fileName.lastIndexOf(".") != -1){
				extentionName = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
			}
			
			extentionName = extentionName.toLowerCase(); //没有扩展名的url
			
			buffImg = ImageIO.read(is);
			if(buffImg == null){
				return null;
			}
			BufferedImage compressPic = ImageUtils.compressPicMin(buffImg, width, height);//图片等比缩放
			if(compressPic != null){ //图片已压缩
				output = new FileOutputStream(urlPath);
				ImageUtils.write(compressPic, extentionName, output);//图片输出
			}
		}finally {
			if(output != null){
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(is != null){
				is.close();
			}
		}
		return "";
	}
	
	
	
	//保存路径获取
	private   String getFolder(String rootPath) {
		// 图片上下文
		String imgContext = PropertyFileUtil.get("imgContext");
		String dateStr = DateUtil.getDate();
		String imgDir = imgContext + "/" + dateStr + "/";
		String path = rootPath + imgDir;
		File dir = new File(path);
		if (!dir.exists()) {
			try {
				dir.mkdirs();
			} catch (Exception e) {
				return "";
			}
		}
		return path;
	}

	


	public int getWidth() {
		return width;
	}


	public void setWidth(int width) {
		this.width = width;
	}


	public int getHeight() {
		return height;
	}


	public void setHeight(int height) {
		this.height = height;
	}


	public static Logger getLogger() {
		return logger;
	}


	public static void setLogger(Logger logger) {
		ImageCompressionTask.logger = logger;
	}
	
	public static void main(String args[]){
		String abc = "abc";
		String ab2 = "abc";
		System.out.print(abc==ab2);
	}
}


3.图片等比缩放,先要设置宽和高
package com.rmyy.web.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Description: 图片工具类
 *
 * @author KangWei
 * @Date 11-2-23
 */
public class ImageUtils {
	private static final Logger log = LoggerFactory.getLogger(ImageUtils.class);
	
	
	/**1111
	 * 将图片写入输出流
	 *
	 * @param image
	 *            BufferedImage 对象
	 * @param format
	 *            输出格式
	 * @param output
	 *            输出流
	 * @throws java.io.IOException
	 *             IO异常
	 */
	public static void write(BufferedImage image, String format,
			OutputStream output) throws IOException {
		ImageIO.write(image, format, output);
	}

	

	
	/**111
	 * 图片等比缩放,先要设置宽和高
	 *
	 * @param image
	 * @param
	 * @throws java.io.IOException
	 */
	public static BufferedImage compressPicMin(BufferedImage image,
			int outputWidth, int outputHeight) throws IOException {
		if (image == null) {
			return null;
		}

		if (image.getWidth(null) < outputWidth
				&& image.getHeight(null) < outputHeight) {
			return image;
		}

		int newWidth;
		int newHeight;
		// 为等比缩放计算输出的图片宽度及高度
		double rate1 = ((double) image.getWidth(null)) / (double) outputWidth;
		double rate2 = ((double) image.getHeight(null)) / (double) outputHeight;
		// 根据缩放比率大的进行缩放控制
		double rate = rate1 < rate2 ? rate1 : rate2;
		newWidth = (int) (((double) image.getWidth(null)) / rate);
		newHeight = (int) (((double) image.getHeight(null)) / rate);

		BufferedImage tag = new BufferedImage(newWidth, newHeight,
				BufferedImage.TYPE_INT_RGB);
		/*
		 * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
		 */
		tag.getGraphics().drawImage(
				image.getScaledInstance(newWidth, newHeight,
						Image.SCALE_SMOOTH), 0, 0, null);
		return tag;
	}

	
}




4.系统性工具类
package com.rmyy.web.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 系统属性工具类
 * 
 * @author HenryYan
 */
public class PropertyFileUtil {

	private static Properties config = null;
	private static Logger logger = Logger.getLogger(PropertyFileUtil.class);
	
	static {
		InputStream in = PropertyFileUtil.class.getClassLoader().getResourceAsStream("application.properties");
		config = new Properties();
		try {
			config.load(in);
			in.close();
		} catch (IOException e) {
			logger.error("加载系统参数配置异常========", e);
		}
	}

	/**
	 * 根据key 得到value
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		try {
			String value = config.getProperty(key);
			return value;
		} catch (Exception e) {
			logger.error("系统参数配置错误========", e);
			return null;
		}
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值