struts2文件和图片上传-最完整的

准备:导入commons-io-2.0.1.jar和commons-fileupload-1.2.1.jar,版本可以不一样


前台:
      <struts:form action="weiboAction!uploadPicture.shtml" enctype="multipart/form-data" namespace="/" method="post">
		<struts:file name="image" label="文件"></struts:file>
    	<struts:submit value="上传"/>
    </struts:form>




后台:
	/**
	 * 作者:刘鹏
	 * 时间:2013-07-07
	 * 描述:微博列表中的图片和文件上传显示
	 * @return
	 */
	
/*****************以下为上传部分*******************************/
	private File image;                        //得到上传的文件
	private String imageFileName;              //得到文件的名称,写法是固定的
	private String imageContentType;		   //得到文件的类型


	public String getImageContentType() {
		return imageContentType;
	}
	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}
	public String getImageFileName() {
		return imageFileName;
	}
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	public File getImage() {
		return image;
	} 
	public void setImage(File image) {
		this.image = image;
	}
	public String addUI(){
		return SUCCESS;
	}
	public String uploadPicture(){
		Weibo model = getModel();
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = simpleDateFormat.format(date);
		model.setTime(dateString);
		HttpServletRequest request = ServletActionContext.getRequest() ;
		HttpServletResponse response = ServletActionContext.getResponse();
		
		//以下为图片上传部分
		//保存到根目录下的uploadImages文件夹下
		String realPath = ServletActionContext.getServletContext().getRealPath("/uploadImages");    //取得真实路径
		System.out.println(realPath);
		System.out.println(imageFileName);
		System.out.println(imageContentType);
		
		//自动命名
	if(imageFileName!=null && imageFileName.length()!=0){
		Random random = new Random(99999);
		int tempInt = random.nextInt();
		Date datenew = new Date();
		SimpleDateFormat simpleDateFormatnew = new SimpleDateFormat("yyyyMMddhhmmss");
		int last = imageFileName.lastIndexOf(".");
		String head = imageFileName.substring(0,last);
		String type = imageFileName.substring(last);
		imageFileName = simpleDateFormatnew.format(datenew) + tempInt + type;
		System.out.println("新的文件名称是:"+imageFileName);
		
		//创建父文件夹
		if(image!=null){
		File saveFile = new File(new File(realPath), imageFileName);
		if(!saveFile.getParentFile().exists()){     //如果Images文件夹不存在
			saveFile.getParentFile().mkdirs();      //则创建新的多级文件夹
			
		}
		try {
			FileUtils.copyFile(image, saveFile);     //保存文件
			ActionContext.getContext().put("message", "上传成功!");
			
			String path = request.getContextPath();
			String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
			String picturePath = basePath +"uploadImages"+"/"+imageFileName;
			model.setImage(picturePath);
			
			//request.setAttribute("uploadsuccess", imageFileName);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		}
		}else{
			model.setImage("");
		}
	
		//增加微博的时候将当前登录人对应的头像信息路径加入到微博表中(此时model中已经存在userName)
		String picturePathString = getiWeiboService().selectPicturePath(model);
		model.setAvatar(picturePathString);
		getiWeiboService().insertWeibo(model);
		
		try {
			response.sendRedirect("jsp/weibo/index.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
			}
	
	
/*****************以上为上传部分*******************************/


后台将图片的地址保存到数据库中
	//先从数据库中将所有数据读出来,放入到request中
	request.setAttribute("weibotest", list);


前台使用OGNL语言读取出图片地址,并且显示图片
	<s:iterator value="#request.weibotest" var="user">
		<s:property value="#user.getContent()"/>
		<img src ='<s:property value ="#user.getImage()" />' width="200">     //显示图片
	</s:iterator>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值