首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

struts2实现文件图片下传

2012-12-20 
struts2实现文件图片上传我的JSP页面代码如下: bodyform namemyform methodpost action/s2/use

struts2实现文件图片上传
我的JSP页面代码如下:

<body><form name="myform" method="post" action="/s2/user/fileAction.action" enctype="multipart/form-data">             <table align="center">                   <tr>                  <td colspan="2">文件上传</td>             </tr>             <tr>                  <td>上传的文件:</td>                  <td><input type="file" name="myfile"/></td>             </tr>             <tr>                  <td>用户编号:</td>                  <td><input type="text" name="userId"/></td>             </tr>             <tr>                  <td>用户姓名:</td>                  <td><input type="text" name="userName"/></td>             </tr>             <tr>                 <td colspan="2"><input type="submit" value="上传"/></td>             </tr>             </table>     </form></body>



我的Action如下:
package com.xll.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import com.opensymphony.xwork2.ActionSupport;public class FileAction extends ActionSupport{public File myfile;public String myfileFileName;public String myfileContentType;public String userId;public String userName;        public String execute(){        InputStream in = null;        OutputStream out = null;                try {in = new FileInputStream(myfile);out = new FileOutputStream("d:/temp/"+myfileFileName);int str = -1;while((str=in.read()) != -1){out.write(str);//str = in.read();}} catch (Exception e) {e.printStackTrace();} finally{try {out.flush();in.close();out.close();} catch (Exception e) {e.printStackTrace();}}        System.out.println("userId="+userId+",userName="+userName);        System.out.println("myfileFileName="+myfileFileName+",myfileContentType="+myfileContentType);        return this.SUCCESS;        }}


struts.xml中的配置:
<action name="fileAction" name="code"><constant name="struts.i18n.encoding" value="gb2312"></constant>


页面编码统一为gbk  上传后的文件未出现乱码!

热点排行