项目____上传文件
来源:互联网 发布:vs2017写c语言 编辑:程序博客网 时间:2024/05/01 02:39
上传单个文件:
现在使用的方法:
配置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" onkeyup="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" onkeyup="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" onkeyup="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];
0 0
- 项目____上传文件
- 夸项目上传文件
- Linux笔记____文件管理
- SVN上传项目.a文件上传不了
- github上传文件 项目管理 上传github
- 项目管理____敏捷式项目管理
- 【项目实战】--Office文件上传
- Github的项目文件上传
- Web项目上传文件失败
- ssm项目中的文件上传
- java项目中的文件上传
- ssh项目中的文件上传
- 积分项目关于 文件上传
- git基础上传项目文件
- web项目之文件上传
- 七牛云存储____图片转Base64串上传
- 七牛云存储____七牛js直接上传图片
- 七牛云存储____官方js上传(基于pupload插件)
- windowAPI--手动编写一个消息窗口
- TSM备份Oracle报错(rman-06012,ANR1401W)
- docker 报错:x509: certificate has expired or is not yet valid
- JavaScript高级程序设计之面向对象的程序设计之继承之寄生组合式继承第6.3.6讲笔记
- 二叉搜索树的后序遍历序列
- 项目____上传文件
- 一些支持向量机(SVM)的开源代码库的链接及其简介
- LeetCode---Maximum Subarray
- Android应用方法数查看,查看size是否超过65k
- ScheduledThreadPoolExecutor
- android fragment和activity的区别
- 由 基本数据型态转换成 String,由 String 转换成 数字的基本数据型态
- springMVC教程中级(四)Controller中级篇2(传图、json)
- python learn: 字符串