文件上传与下载

---恢复内容开始---

1、单文件上传:

   1)单文件上传(文件上传: 导入两个包commons-fileupload-1.3.1.jarcommons-io-2.4.jar)

      首先在JSP中enctype=“multipart/form-date”

 <s:form action="login_login" enctype="multipart/form-data" namespace="/" method="post" theme="xhtml">

       <s:textfield name="username" label="用户名"> </s:textfield>

       <s:password name="pwd" label="密码框"></s:password>

        <s:file name="photo"></s:file>

        <s:submit value="提交"></s:submit>      

     </s:form>

       2)在action中增加photo的属性

publicclassUserActionextends ActionSupport{

private File photo; //记得生成get set 方法

   //命名规则:表单中的名字photo+FileName,表示上传上来的文件名

private String photoFileName;

private String photoContentType;

public String save() throws IOException{

   System.out.println("save....");

   System.out.println("username="+username);

   //构建一个新文件的目录

   File file=new File("c:/upload/"+photoFileName);

     //把上传上来的文件复制到c盘中   

       FileUtils.copyFile(photo, file);

return "list";

   }

...

 2、多文件上传:

     1)在JSP页面中

 

 <s:form action="login_login" enctype="multipart/form-data" namespace="/" method="post" theme="xhtml">

       <s:textfield name="username" label="用户名"> </s:textfield>

       <s:password name="pwd" label="密码框"></s:password>

        <s:file name="photo"></s:file>

    <s:file name="photo"></s:file>

         <s:file name="photo"></s:file>

        <s:submit value="提交"></s:submit>

     </s:form>

2)action中用List或用数组来接收

publicclassUserActionextends ActionSupport {

private List<File> photo;

private List<String> photoFileName;

private List<String> photoContentType;

public String save() throws IOException{

   System.out.println("save....");

   System.out.println("username="+username);

if(photo!=null){

for(int i=0;i<photo.size();i++){

      File newfile=new File("c:/upload/"+photoFileName.get(i));

      FileUtils.copyFile(photo.get(i), newfile);

    }

   }

return "list";

   }

---恢复内容结束---

1、单文件上传:

   1)单文件上传(文件上传: 导入两个包commons-fileupload-1.3.1.jarcommons-io-2.4.jar)

      首先在JSP中enctype=“multipart/form-date”

 <s:form action="login_login" enctype="multipart/form-data" namespace="/" method="post" theme="xhtml">

       <s:textfield name="username" label="用户名"> </s:textfield>

       <s:password name="pwd" label="密码框"></s:password>

        <s:file name="photo"></s:file>

        <s:submit value="提交"></s:submit>      

     </s:form>

       2)在action中增加photo的属性

publicclassUserActionextends ActionSupport{

private File photo; //记得生成get set 方法

   //命名规则:表单中的名字photo+FileName,表示上传上来的文件名

private String photoFileName;

private String photoContentType;

public String save() throws IOException{

   System.out.println("save....");

   System.out.println("username="+username);

   //构建一个新文件的目录

   File file=new File("c:/upload/"+photoFileName);

     //把上传上来的文件复制到c盘中   

       FileUtils.copyFile(photo, file);

return "list";

   }

...

 2、多文件上传:

     1)在JSP页面中

 

 <s:form action="login_login" enctype="multipart/form-data" namespace="/" method="post" theme="xhtml">

       <s:textfield name="username" label="用户名"> </s:textfield>

       <s:password name="pwd" label="密码框"></s:password>

        <s:file name="photo"></s:file>

    <s:file name="photo"></s:file>

         <s:file name="photo"></s:file>

        <s:submit value="提交"></s:submit>

     </s:form>

2)action中用List或用数组来接收

publicclassUserActionextends ActionSupport {

private List<File> photo;

private List<String> photoFileName;

private List<String> photoContentType;

public String save() throws IOException{

   System.out.println("save....");

   System.out.println("username="+username);

if(photo!=null){

for(int i=0;i<photo.size();i++){

      File newfile=new File("c:/upload/"+photoFileName.get(i));

      FileUtils.copyFile(photo.get(i), newfile);

    }

   }

return "list";

   }

 

3、如何控制文件上传的大小及类型呢?

      1)找到文件上传相对应的拦截器FileUploadInterceptor

  2)在struts.xml配置文件中引入文件上传的拦截器

 

<struts>

 <!-- ture:表示修改了这个配置文件 就不用重新启动tomcat -->

       <constant name="struts.configuration.xml.reload" value="true"></constant>

       <!-- 限制所有上传文件的总大小 -->

       <constant name="struts.multipart.maxSize" value="2097152"></constant>

       <package name="userpack" extends="struts-default" namespace="/user" strict-method-invocation="false">

         <action name="user_*" class="cn.hd.controller.UserAction" method="{1}">

            <!-- 引用文件上传的拦截器 -->

            <interceptor-ref name="fileUpload">

               <!-- 单个文件的最大值  单位b 不超过1M-->

               <param name="maximumSize">1048576</param>

               <!-- 限制文件的后缀名

               <param name="allowedExtensions">.doc,.png</param>-->

               <!-- 限制文件的类型 -->

               <param name="allowedTypes">image/bmp,image/png</param>

            </interceptor-ref>

            <interceptor-ref name="defaultStack"></interceptor-ref>

            <result name="list" >/index.jsp</result>

            <result name="success" >/ui.jsp</result>

           <!--记得要加上input返回视图-->

            <result name="input" >/login.jsp</result>

         </action>

     </package>

  </struts>  

3)在页面中提示相关信息,则要记得加上这句话:<s:actionerror/>

二、下载

1、在jsp中写一个“下载”按钮

 <a href="user/user_download?filename=我是.png">下载</a>

2、action中加上download方法,这个方法里没有做任何事情,只是用来转到result结果,并要加上一个接收要下载的文件名的属性filename,

//接收要下载的文件名

  private String filename; //生成get set方法

  public String download(){

   System.out.println("进行文件下载:"+filename);

return "download";

   }

2、struts.xml配置文件中加入

<result name="download" type="stream">

               <!-- 文件下载的类型 -->

                <param name="contentType">application/octet-stream</param>

                 <!-- 下载的入口:告诉它到哪里下载 -->

                <param name="inputName">inputStream</param>

                 <!-- 下载的方式: attachment表示附件方式即会弹出一个下载窗口-->

                <param name="contentDisposition">attachment;filename=${filename}</param>

            </result>

 4、在<param name="inputName">inputStream</param>里值inputStream,要在对应的action中生成它的Get方法,如getInputStream(),并返回输入流

//文件下载的入口

public InputStream getInputStream() {

   File file=new File("c:/upload/"+filename);

   InputStream is;

try {

is = new FileInputStream(file);

return is;

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

//e.printStackTrace();

System.out.println("找不到这个文件,出错了啦");

}

returnnull;

   }

 

   5、如果下载的文件名是中文,则要记得设置两个地方:

      1get方式提交请求,要在tomcat目录下的conf目录下server.xml加上编码URIEncoding="UTF-8":

 <Connector connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

  2)在接收下载文件的文件名filename属性的get方法中更改文件名显示的编码方式为ISO-8859-1

这样下载时弹出的下载框就不会出现乱码问题

   public String getFilename(){

try {

String str=new String(filename.getBytes(), "ISO-8859-1");

   return str;

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

posted on 2016-12-12 15:59  Joyous丶  阅读(370)  评论(0编辑  收藏  举报

导航