Struts文件上傳和下載

          jsp
          <%@ page language="java" contentType="text/html; charset=GBK" %>
          <html>
          <head>
              
          <meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
              
          <title></title>
          </head>
          <body>
          <br>
          <html:form action="/datamanage.data.upload.do" method="post" enctype="multipart/form-data" target="_parent">
          <table width="45%" border="0" align="center" cellpadding="0" cellspacing="0" class="table_box">
              
          <tr>
                  
          <td align="center" class="title_box" colspan="2">
                      文件上傳
                  
          </td>
              
          </tr>
              
          <tr>
                  
          <td>
                      文件:
                  
          </td>
                  
          <td>
                      
          <html:file property="files" styleClass="text_2003"/>
                  
          </td>
              
          </tr>
              
          <tr>
                  
          <td>
                      描述:
                  
          </td>
                  
          <td>
                      
          <textarea rows="3" cols="40" class="text_2003" name="data_desc"></textarea>
                  
          </td>
              
          </tr>

              
          <tr>
                  
          <td class="bottom_box" colspan="2" align="center">
                      
          <input type="button" value="上傳" styleClass="button_2003" onclick="checkForm();"/>&nbsp;&nbsp;&nbsp;
                      
          <input type="button" onclick="cancle();" value="取消" styleClass="button_2003"/>
                  
          </td>
              
          </tr>
              
          </html:form>
          </body>
          </html>


          DataUploadForm
          package eoms.datamanage.struts.form;

          import eoms.common.base.BaseForm;
          import org.apache.struts.upload.FormFile;

          public class DataUploadForm extends BaseForm {
              
          private FormFile files;
              
          private String type_id;
              
          private String data_path;
              
          private String data_desc;

              
          public String getType_id() {
                  
          return type_id;
              }


              
          public void setType_id(String type_id) {
                  
          this.type_id = type_id;
              }


              
          public FormFile getFiles() {
                  
          return files;
              }


              
          public void setFiles(FormFile file) {
                  
          this.files = file;
              }


              
          public String getData_path() {
                  
          return data_path;
              }


              
          public void setData_path(String data_path) {
                  
          this.data_path = data_path;
              }


              
          public String getData_desc() {
                  
          return data_desc;
              }


              
          public void setData_desc(String data_desc) {
                  
          this.data_desc = data_desc;
              }

          }


          文件下載Action
          package eoms.datamanage.struts.action;

          import eoms.common.base.BaseAction;
          import eoms.datamanage.dao.DataTypeDAO;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import java.io.*;
          import java.net.URLEncoder;

          public class DataDownAction extends BaseAction {

              
          public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
                  DataTypeDAO typeDao 
          = (DataTypeDAO) this.getBean("DataTypeDAO");

                  String type_id 
          = request.getParameter("type_id");
                  String data_name 
          = request.getParameter("data_name");
                  String path
          =typeDao.getDataTypeById(Integer.parseInt(type_id)).getData_path()+"/"+data_name;

                  BufferedInputStream bis 
          = null;
                  BufferedOutputStream bos 
          = null;
                  OutputStream fos 
          = null;
                  InputStream fis 
          = null;

                  String filepath 
          = path;
                  System.out.println(
          "文件路徑" + filepath);
                  File uploadFile 
          = new File(filepath);
                  fis 
          = new FileInputStream(uploadFile);
                  bis 
          = new BufferedInputStream(fis);
                  fos 
          = response.getOutputStream();
                  bos 
          = new BufferedOutputStream(fos);
                  
          //這個(gè)就就是彈出下載對(duì)話框的關(guān)鍵代碼
                  response.setHeader("Content-disposition",
                          
          "attachment;filename=" + URLEncoder.encode(data_name,"GBK"));
                  
          int bytesRead = 0;
                  
          //用輸入流進(jìn)行先讀,然后用輸出流去寫(xiě)
                  byte[] buffer = new byte[8192];
                  
          while ((bytesRead = bis.read(buffer, 08192)) != -1{
                      bos.write(buffer, 
          0, bytesRead);
                  }

                  bos.flush();
                  fis.close();
                  bis.close();
                  fos.close();
                  bos.close();

                  
          return null;
              }

          }


          文件上傳Action
          package eoms.datamanage.struts.action;

          import com.metarnet.security.model.User;
          import eoms.common.Constants;
          import eoms.common.base.BaseAction;
          import eoms.datamanage.dao.DataDAO;
          import eoms.datamanage.dao.DataTypeDAO;
          import eoms.datamanage.model.DataModel;
          import eoms.datamanage.model.DataTypeModel;
          import eoms.datamanage.struts.form.DataUploadForm;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.upload.FormFile;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import java.io.FileOutputStream;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.util.HashMap;
          import java.util.Map;

          public class DataUploadAction extends BaseAction {

              
          public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
                  User user 
          = (User) request.getSession().getAttribute(
                          Constants.SESSION_USER_KEY);
                  DataTypeDAO typeDao 
          = (DataTypeDAO) this.getBean("DataTypeDAO");
                  DataDAO dataDao 
          = (DataDAO) this.getBean("DataDAO");
                  DataUploadForm theForm 
          = (DataUploadForm) form;
                  FormFile file 
          = theForm.getFiles();//取得上傳的文件
                  String filePath = theForm.getData_path();//上傳到指定的文件夾中
                  String fileDesc = theForm.getData_desc();
                  String type_id 
          = theForm.getType_id();

                  
          try {
                      InputStream stream 
          = file.getInputStream();//把文件讀入

                      OutputStream bos 
          = new FileOutputStream(filePath + "/" +
                              file.getFileName());
          //建立一個(gè)上傳文件的輸出流

                      
          int bytesRead = 0;
                      
          byte[] buffer = new byte[8192];
                      
          while ((bytesRead = stream.read(buffer, 08192)) != -1{
                          bos.write(buffer, 
          0, bytesRead);//將文件寫(xiě)入服務(wù)器
                      }

                      bos.close();
                      stream.close();

                      DataTypeModel typeModel 
          = typeDao.getDataTypeById(Integer.parseInt(type_id));
                      
          //判斷是否已經(jīng)存在
                      Map map = new HashMap();
                      map.put(
          "type_code", typeModel.getData_type_code());
                      map.put(
          "data_name", file.getFileName());
                      DataModel data 
          = dataDao.getDataByTypeAndName(map);
                      
          if (data == null{
                          
          //將資料信息保存進(jìn)數(shù)據(jù)庫(kù)
                          DataModel model = new DataModel();
                          model.setData_type_code(typeModel.getData_type_code());
                          model.setData_name(file.getFileName());
                          model.setData_type(typeModel.getData_type_name());
                          model.setData_desc(fileDesc);
                          model.setData_author(user.getUserId());
                          dataDao.addNewData(model);
                      }

                  }
           catch (Exception e) {
                      System.err.print(e);
                  }


                  request.setAttribute(
          "type_id", type_id);
                  
          return mapping.findForward("success");
              }


          }

          posted on 2011-03-25 16:39 Mr.lu 閱讀(196) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 华坪县| 宁陕县| 柞水县| 海安县| 茌平县| 西乡县| 石柱| 东城区| 泗洪县| 泸州市| 叶城县| 铜川市| 年辖:市辖区| 张家港市| 周口市| 洞头县| 焦作市| 汉沽区| 云南省| 浦江县| 旬阳县| 宿迁市| 天峻县| 瑞昌市| 左贡县| 游戏| 云霄县| 邓州市| 手游| 和林格尔县| 蒲城县| 乌鲁木齐市| 沙坪坝区| 九龙城区| 吕梁市| 汽车| 鹤庆县| 阿坝县| 剑川县| 龙山县| 余干县|