上传单个文件:
现在使用的方法:
配置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";
}
最后注意一下,如果通过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];