1. 程式人生 > >關於在一個form表單裡同時上傳多個檔案和文字資訊的解決方案。。。

關於在一個form表單裡同時上傳多個檔案和文字資訊的解決方案。。。



執行環境:tomcat5.0.30+springframework

步驟:
1。定義一個form表單index.jsp和error.jsp
**************************************index.jsp*************************************************
<%@ page contentType="text/html; charset=GBK" %>

<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="up.htm" enctype="multipart/form-data">
<input type="file" name="file"/><br>
<input type="file" name="file1"/><br>
<TEXTAREA name="description" ROWS=5 COLS=30>
</TEXTAREA><br>
<input type="submit"/>
</form>
</body>
</html>
**************************************error.jsp**********************************
<%@ page language="java" isErrorPage="true" %>

<head><title>Doh!</title></head>

An Error has occurred in this application.  
2。定義控制類FileUploadController和commandClass UploadBean
**************************FileUploadController.java*************************************************************
public class FileUploadController extends SimpleFormController {
    private static Log log = LogFactory.getLog(getClass());
    public ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse res, Object obj, BindException errors)
            throws Exception {
        UploadBean bean = (UploadBean) obj;
        if(bean == null)return new ModelAndView("error");
        
        byte[] file = bean.getFile();
        byte[] file1 = bean.getFile1();
        String[] d= bean.getDescription();
        
        ==================================================
        在這裡就可以對上面得到的資料進行處理,可以把上傳的檔案內容儲存到本地或
        者資料庫,對文字區的說明文字也可以進行處理。其最大的優點就是把request裡面
        的資料繫結到了commandClass,有利於使用spring的驗證機制和其他的輔助功能。    
        
        ==================================================
        
    }
    //註冊一個spring的編輯器非常重要,沒有這個方法,上傳將不能進行
    protected void initBinder(HttpServletRequest request,
            ServletRequestDataBinder binder) throws ServletException {
        binder.registerCustomEditor(byte[].class,
                new ByteArrayMultipartFileEditor());
    }
    
}
****************************************UploadBean.java***********************************



public class UploadBean {
    private String[] description;
    private byte[] file;
    private byte[] file1;
    public void setDescription(String[] description) {
        this.description = description;
    }

    public String[] getDescription() {
        return this.description;
    }
    public void setFile(byte[] file) {
        this.file = file;
    }

    public byte[] getFile() {
        return file;
    }
    public void setFile1(byte[] file1){
        this.file1 = file1;
    }
    public byte[] getFile1(){
        return file1;
    }
}

================================================
3.配置web.xml檔案
<servlet>
        <servlet-name>simple</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>simple</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
4。配置simple-servlet.xml檔案
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="multipartResolver"
       class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        
</bean>
    
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>


    <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">        
    <property name="mappings">            
        <props>                
        <prop key="/up.htm">simpleController</prop>            
        </props>        
    </property>    
    </bean>
        <bean id="simpleController" class="SimpleController">
            <property name="commandClass"><value>UploadBean</value></property>
       </bean>   
       
    
</beans>

5.配置tomcat,新增所需的jar檔案,commons-logging.jar;servlet-api.jar;spring.jar;jstl.jar
啟動tomcat,預覽:http://localhost:8080/(根目錄名)/index.jsp
如果有問題,請參照前面我blog裡面關於單個檔案上傳的文章。

說明幾點:
a。表單裡面的name值,必須和commandClass裡面的byte[]的名稱相對應。
b. 控制類裡面一定要註冊一個編輯器。如果沒有initBinder()則上傳不能進行。
c。發現spring一個問題,如果上傳的檔案過大(超過30M),會丟擲一個
java.lang.OutOfMemoryError: Java heap space
異常,現在不知道怎麼解決。
d。spring上傳的速度一般維持在兩百多k,不知道有沒有辦法可以突破這個瓶頸?
e。希望有經驗的XDJM可以和我探討上面的c,d及spring有關的問題,可以給我留言,或發信
[email protected]

相關推薦

關於在一個form同時檔案文字資訊解決方案

執行環境:tomcat5.0.30+springframework步驟:1。定義一個form表單index.jsp和error.jsp**************************************index.jsp************************

一個form中,接收相同的name欄位引數

一般我們在同一個表單中,都會提交不同的name欄位。可是,我遇到了提交相同欄位的情況,例子如下: <form action="test_receive.php" method="post"> <div> <

php 在linux 同時檔案時報錯 SplFileObject::__construct(/tmp/phpvfQPvQ):

分析:這個不是上傳檔案的最大值的問題,而是由於linux系統自動清除臨時檔案的願意造成的   thinkphp\library\think\File.php 的建構函式加個判斷 if(file_exists($filename)) 簡單解決報錯可以讓你用起來 不知道有沒有其他其他不

asp.net簡單例項——同時檔案

     之前在網站上看到的一些上傳檔案的功能,感覺還是蠻方便的,這次自己利用asp.net中的HttpFileCollection類做了一個簡單的例子。廢話不多說,下面看操作。     首先,新增一個新的web窗體,在窗體上直接拖拽這幾個控制元件:一個Panel作為容器、

input File實現同時檔案

預設的呼叫方式: <input type="file" name="filename"/> 可實現上傳單個檔案,但無法多選確認。 html在input[type='file']中給我們提

使用ajaxfileupload同時檔案

ajaxFileupload這個外掛用於上傳檔案,不過它預設是隻能上傳一個檔案。通過修改其原始碼,可以方便地實現多個檔案上傳。 ajaxFileupload的原理挺簡單的,它將使用者指定(通過ID)的file input新增到form裡面,然後將form提交。我們只需要

通過HttpClient4.5模擬Form文件

cti main [] pmi 通過 commons finally 請求 str public static void main(String[] args) { CloseableHttpClient httpclient = HttpClients.

form文件

pen form表單 創建 for method 接收 multi 服務器 ava 創建一個form表單 #上傳文件的時候,enctype屬性改成multipart/form-data <from action=‘‘ method=‘post‘ enctype=‘

Ajax(form文件、請求頭之contentType、Ajax傳遞json數據)

ati 沒有 服務端 內容 click 寫入 ESS mit 上傳 form表單文件上傳 上菜 file_put.html <form action="" method="post" enctype="multipart/form-data"> {#

記錄一次使用form完成圖片功能(相容ie9)

1.前臺頁面 <div class="dform_con" style="position: relative;" id="uploadform"> <iframe id="file_upload_return" style="d

form提交方式圖片到伺服器

springMVC為檔案上傳提供了直接的支援,這種支援是通過MultipartResolver實現的,實現類為CommonsMultipartResovler. 依賴:commons-fileupload-1.2.jar     commons-io-1.

form提交批量檔案,不重新整理不跳轉頁面

jsp: <form action="inImg.do" method="post" enctype="multipart/form-data" target="nm_iframe"> <table> <tr>

php form提交 然後圖片到七牛

require_once APP_ROOT.'/vendor/config.php'; use Qiniu\Auth; use Qiniu\Storage\UploadManager;//引入上傳類 /

struts2使用form無重新整理檔案

這幾天在做專案時有用到一個form表單來上傳多個檔案,並且要求不能重新重新整理。當時在做的時候由於不是很瞭解這個東西,導致還是走了一些彎路的,現在把程式碼貼出來,供大家一起交流 html程式碼 <div id="book" style="display:none; b

Post同時張圖片資料例項

1.通過http協議,post同時上傳多張圖片和資料到伺服器上。 2.IOS開發中前端採用xcode 7.1 swift語言。 3.伺服器端採用asp.net,asp實現。 一、下面先看看前端程式碼。 1.先把需要上傳的圖片存在img陣列中。 2.im

C# HTTP系列13 以form-data方式檔案以及鍵值對集合到遠端伺服器

系列目錄     【已更新最新開發文章,點選檢視詳細】 類似於以下場景,將表單中的使用者資訊(包含附件)上傳到伺服器並儲存到資料庫中, <form id="form1" runat="server" action="UserManageHandler.as

微信小程式(檔案)

微信小程式上傳(多個檔案上傳) /** * 上傳照片//選擇圖片時限制9張,如需超過9張,同理亦可參照此方法上傳多張照片 */ uploadImg:function(){ var that = this; wx.chooseImage({ count

ajaxfileupload.js 檔案

上傳單個檔案 JS $.ajaxFileUpload({ url :"", secureuri : false, fileElementId : 'img', --傳入file ID data:

input型別檔案(selenium+Python)

一般上傳方法分為兩種:input型別和非input型別,這裡具體分析input型別。 html檔案: 【上傳圖示】按鈕元素定位中包含name=“file”   type="file"  果斷採用input+sendkeys模式。 一、上傳一張圖片,上程式碼: 解釋

vue檔案,附件其他資料一起給後臺

前端: 實現多圖上傳主要用到以下兩個屬性:        <el-form-item label="附件上傳" label-width="80px">       <el-f