struts1.3.8多文件上傳以及文件下載源碼

struts多文件上傳以及文件下載源碼

這裏使用Struts1.3.8舉例,版本很老了,但是基本的思路是一樣的,新的Struts做了一些改良

多文件上傳

jsp界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/UpFile.do" method="post" enctype="multipart/form-data">
  上傳用戶:<input type="text" name="username"><br>
  <!-- 使用UpFileFormBean的upfiles保存 -->
  上傳文件1:<input type="file" name="upfiles[0]"><br>
  上傳文件2:<input type="file" name="upfiles[1]"><br>
  <input type="submit" value="上傳"> 
   </form><br>
   <a href="${pageContext.request.contextPath }/DownFile.do">下載oracle.gif</a>
</body>
</html>

保存提交數據的formbean

public class UpFileFormBean extends ActionForm{
   private String username;
   private List<FormFile> upfiles=new ArrayList<>();//這裏最好使用list來保存,使用數組([])比較麻煩
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}

public FormFile getUpfiles(int index) {
    return upfiles.get(index);
}
public void setUpfiles(int index,FormFile file) {
    upfiles.add(file);
}
public List<FormFile> getAll(){
    return upfiles;
}
}

圖片上傳action

public class UpFileAction extends Action{

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        UpFileFormBean formBean=(UpFileFormBean) form;
        System.out.println("上傳用戶:"+formBean.getUsername());

        List<FormFile> all=formBean.getAll();
        System.out.println(all.size());
        for(FormFile formFile:all){
            String filename= formFile.getFileName();
            InputStream in=formFile.getInputStream();
            FileOutputStream out=new FileOutputStream("D:\\"+filename);
            int len=0;
            byte buffer[]=new byte[1024];
            while((len=in.read(buffer))>0){
                out.write(buffer,0,len);
            }
            in.close();
            out.close();
        }
        return super.execute(mapping, form, request, response);
    }
}

圖片下載action,這裏繼承了Struts的DownloadAction用來下載

public class DownFileAction extends DownloadAction {

    @Override
    protected StreamInfo getStreamInfo(ActionMapping arg0, ActionForm arg1, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        //通知瀏覽器以下載形式訪問

response.setContentType("application/x-download");
        response.setHeader("Content-Disposition", "attachment;filename=oracle.gif");
        response.setHeader("content-type", "zip"); 
        String path=request.getSession()
                .getServletContext()
                .getRealPath("/download/oracle.gif");
        return new DownloadAction.FileStreamInfo("image/gif", new File(path));
    }
}  

WEB-INF下的struts-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
  "http://struts.apache.org/dtds/struts-config_1_3.dtd">


<struts-config>

 <form-beans>
<form-bean name="UpFileFormBean" type="com.wz.formbean.UpFileFormBean">
</form-bean>
 </form-beans>
 <action-mappings>
  <action path="/UpFile"
   type="com.wz.action.UpFileAction"
   name="UpFileFormBean"
   scope="request">
  </action>

  <action path="/DownFile"
   type="com.wz.action.DownFileAction"
 >
  </action>
 </action-mappings>
 <!-- 此處設置文件上傳大小 可以以K,M,G爲單位,默認250M
  如果上傳文件超出了最大值,Struts不會把上傳數據封裝到FormFile中,也就是
 FormFile=null,在程序中根據它是否爲空,提示用戶文件是否超出大小
  -->
 <controller processorClass="org.apache.struts.action.RequestProcessor" maxFileSize="1M"></controller>

</struts-config>

WEB-INF下的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>strutsFileUpAndDown</display-name>
  <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- 配置Struts的核心servlet -->
  <servlet>
    <servlet-name>ActionServlet</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <!-- 設置該servlet啓動時運行 -->
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ActionServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章