项目____上传文件

上传单个文件:

现在使用的方法:

配置struts文件

<span style="white-space:pre">		</span><action name="seedUpload" class="com.csValue.basicInfor.wechatManage.web.WechatAction" method='uploadSeed'>
			<interceptor-ref name="defaultStack" />
			<interceptor-ref name="fileUpload">
				<param name="allowedTypes">
					image/png,image/jpg
                </param>
				<param name="maximumSize">1048576</param>
			</interceptor-ref>
			<result name="success">/WEB-INF/content/basicInfor/wechatManage/wechat-plants.jsp</result>
			<result name="input">/WEB-INF/content/basicInfor/wechatManage/wechat-plants.jsp</result>
		</action>
jsp内容

<s:form action="seedUpload" id="addForm"  method="post"  theme="simple" enctype="multipart/form-data">
										<input type="hidden" id="id" name="id" value=""></input>
            							<table align="center" width="50%" border="1">
                 						<tr>
                     						<td>作物编号</td>
                     						<td><input id="uid" name="uid" readonly></input></td>
                 						</tr>
                 						
                 						<tr>
                     						<td>作物名</td>
                     						<td><s:textfield id="name" name="name"></s:textfield></td>
                 						</tr>
                 						
                 						
                 						<tr>
                     						<td>种子期图片</td>
                     						<td >
                         						<s:file id="seed" name="seed"></s:file>
                     						</td>
                 						</tr>
                 						
                 						<tr>
                     						<td>发芽期天数</td>
                     						<td><s:textfield id="sprout_day" name="sprout_day" οnkeyup="value=value.replace(/[^\d]/g,'')"></s:textfield></td>
                 						</tr>

										<tr>
                     						<td>发芽期图片</td>
                     						<td >
                         						<s:file id="sprout" name="sprout"></s:file>
                     						</td>
                 						</tr>
                 						
                 						<tr>
                     						<td>成长期天数</td>
                     						<td><s:textfield id="growup_day" name="growup_day" οnkeyup="value=value.replace(/[^\d]/g,'')"></s:textfield></td>
                 						</tr>
                 						
                 						<tr>
                     						<td>成长期图片</td>
                     						<td >
                         						<s:file id="growup" name="growup"></s:file>
                     						</td>
                 						</tr> 
                 						
                 						<tr>
                     						<td>成熟期天数</td>
                     						<td><s:textfield id="ripe_day" name="ripe_day" οnkeyup="value=value.replace(/[^\d]/g,'')"></s:textfield></td>
                 						</tr> 
                 						
                 						<tr>
                     						<td>成熟期图片</td>
                     						<td >
                         						<s:file id="ripe" name="ripe"></s:file>
                     						</td>
                 						</tr>               						
                 						
                 						<tr>
                 <pre name="code" class="html"><span style="white-space:pre">			</span><td><s:reset value=" submit"></s:submit></td>
<td><s:reset value=" reset "></s:reset></td> </tr> </table> </s:form>

 action内容 

public String uploadSeed()throws Exception{    
		
		System.out.println("调用开始");
		String imgpath="upload";
		String img=null;
		
		if(seed!=null){
			InputStream is=new FileInputStream(seed);
			
			String path=ServletActionContext.getServletContext().getRealPath("/");
			
			img=this.getSeedFileName();
			
			File destFile=new File(path+imgpath,img);
			
			OutputStream os=new FileOutputStream(destFile);
			byte[] buffer=new byte[400];
			int length=0;
			while((length=is.read(buffer))>0){
				os.write(buffer,0,length);
			}
			this.data.set("seed_img",imgpath+"/"+img);
			is.close();
			os.close();
		}
		if(sprout!=null){
			InputStream is=new FileInputStream(sprout);
			
			String path=ServletActionContext.getServletContext().getRealPath("/");
			
			img=this.getSproutFileName();
			
			File destFile=new File(path+imgpath,img);
			
			OutputStream os=new FileOutputStream(destFile);
			byte[] buffer=new byte[400];
			int length=0;
			while((length=is.read(buffer))>0){
				os.write(buffer,0,length);
			}
			this.data.set("sprout_img",imgpath+"/"+img);
			is.close();
			os.close();
		}
		if(growup!=null){
			InputStream is=new FileInputStream(growup);
			
			String path=ServletActionContext.getServletContext().getRealPath("/");
			
			img=this.getGrowupFileName();
			
			File destFile=new File(path+imgpath,img);
			
			OutputStream os=new FileOutputStream(destFile);
			byte[] buffer=new byte[400];
			int length=0;
			while((length=is.read(buffer))>0){
				os.write(buffer,0,length);
			}
			this.data.set("growup_img",imgpath+"/"+img);
			is.close();
			os.close();
		}
		if(ripe!=null){
			InputStream is=new FileInputStream(ripe);
			
			String path=ServletActionContext.getServletContext().getRealPath("/");
			
			img=this.getRipeFileName();
			
			File destFile=new File(path+imgpath,img);
			
			OutputStream os=new FileOutputStream(destFile);
			byte[] buffer=new byte[400];
			int length=0;
			while((length=is.read(buffer))>0){
				os.write(buffer,0,length);
			}
			this.data.set("ripe_img",imgpath+"/"+img);
			is.close();
			os.close();
		}
		try{
			data.set("id", uid);
			String id_=data.getString("id");
			System.out.println(id_+"this is try!");
			if(data.getString("id").isEmpty()){
				data.set("id", null);
			}
		}catch (Exception e){
			System.out.println("exception!");
			data.set("id", null);
		}
		this.data.set("name", name);
		this.data.set("sprout_day", sprout_day);
		this.data.set("growup_day", growup_day);
		this.data.set("ripe_day", ripe_day);


		this.wechatManage.uploadPlants(data);
		this.searchPlants();
		return "plants";
	}


经过试验结论:其实不需要重新配置struts中的上传方法。直接在jsp引入file,然后通过button转到相应action处理即可。并且通过此方法可以在提交之前判断对应的文件和字段是否为空。其实都是MVC中的一环。重复了!!


最后注意一下,如果通过button提交,最后不能返回success了!此时的success是返回系统的默认界面,即wechat界面而不是strurs内配置的plants界面了!


_____________________________________________________________________________________________________________________

上传单个文件动态命名后,在名字后面+上上传文件的类型,否则是二进制文件 前端打开后乱码内容:

<span style="white-space:pre">		</span>BufferedInputStream is2 = new BufferedInputStream(new FileInputStream(imgfile));
		String mimeType = URLConnection.guessContentTypeFromStream(is2);
		String[] st = mimeType.split("/");
		img=time+"."+st[1];


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值