春のファイルのアップロードとダウンロード

web.xmlの基本的な構成:

<?XMLバージョン= "1.0"エンコード= "UTF-8" ?> 
< ウェブアプリののxmlns = "http://xmlns.jcp.org/xml/ns/javaee" 
         のxmlns:XSI = "のhttp://www.w3 .ORG / 2001 / XMLスキーマ・インスタンス" 
         のxsi:schemaLocationの=" http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd " 
         バージョン=" 4.0" > 
    < 聞き手> 
            < リスナークラス> org.springframework.web.context.ContextLoaderListener </ リスナクラス> 
        </ リスナー> 

        <
            contextConfigLocation </ PARAM名> 
            < PARAM-値>クラスパス:applicationContext.xmlを</ PARAM値> 
        </ コンテキストのparam > 

        < サーブレット> 
            < サーブレット名> springmvc </ サーブレット名> 
            < サーブレットクラス>組織.springframework.web.servlet.DispatcherServlet </ サーブレットクラス> 
            < INIT-PARAM > 
                < PARAM名> contextConfigLocation </ PARAM名> 
                <PARAM値>クラスパス:springmvc.xml </ PARAM値> 
            </ INIT-PARAM > 
        </ サーブレット> 
        < のservlet-mapping > 
            < サーブレット名> springmvc </ サーブレット名> 
            < のurl-pattern > * .doという</ のurl-pattern > 
        </ のservlet-mapping > 

      <! - 解决中文乱码- > 
          < フィルタ> 
              < フィルタ名> CharacterEncodingFilter </ フィルタ名>
              <フィルタ・クラス> org.springframework.web.filter.CharacterEncodingFilter </ フィルタクラス> 
              < INIT-PARAM > 
                  < PARAM名>エンコード</ PARAM名> 
                  < PARAM値> UTF-8 </ PARAM値> 
              </ INIT-PARAM > 
          </ フィルタ> 
          < のfilter-mapping > 
          < フィルタ名> CharacterEncodingFilter </ フィルタ名> 
          < のurl-pattern > / * </ URLパターン> 
          </ のfilter-mapping > 

</ ウェブアプリ>

 コードのダウンロードとアップロードを以下のJSPページ

アップロード:

< フォームアクション= "upload.do" メソッド= "ポスト" のenctype = "マルチパート/フォームデータ" > 

  <%- 用户名:< 入力名= " ユーザ名"  > 密码:< 入力タイプ= " パスワード" 名前= " パスワード" > - %> 
  文件:< 入力タイプは= "ファイル" = "ファイル" > 
  <%- <入力タイプ= ボタン= " ログイン"  > - %> 
  < ボタン>提出</ ボタン> 
</ フォーム> 


ダウンロード:
< のhref = "download.do?name = $パス{}" >ファイルのダウンロード</ A >

springMVC(springmvcxml)と春(applicationContext.xmlを)プロファイル(メイン設定ファイルの春限り、我々がここで作成すると、何も設定する必要はありませんが、管理せずに、同様です):

<?XMLバージョン= "1.0"エンコード= "UTF-8" ?> 
< のxmlns = "http://www.springframework.org/schema/beans" 
       のxmlns:XSI = "http://www.w3.org/2001 / XMLスキーマ・インスタンス" のxmlns:MVC = "http://www.springframework.org/schema/mvc" 
       のxmlns:コンテキスト= "http://www.springframework.org/schema/context" 
       XSI:のschemaLocation =" HTTP: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvcのhttp://www.springframework .ORG /スキーマ/ MVC /スプリングmvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context。

    コンテキスト:コンポーネント・スキャンベースパッケージ= "com.aaa.springmvc.controller" > </ コンテキスト:コンポーネント・スキャン> 
    < MVC:アノテーション駆動型> </ MVC:アノテーション駆動型> 
    < ビーンID = "multipartResolver" クラス= "org.springframework.web.multipart.commons.CommonsMultipartResolver" > </ > 
    < 豆のクラス= "org.springframework.web.servlet.view.InternalResourceViewResolver" > 
        < プロパティ= "接頭辞" = "" > </ プロパティ> 
        < プロパティ名前= "サフィックス" = "JSP" > </ プロパティ> 
    </ > 
</ >

次のように制御コードがあります:

パッケージcom.aaa.springmvc.controller。

輸入com.aaa.spring.file.util.FileUtil。
輸入com.aaa.springmvc.entity.Users。
輸入org.springframework.stereotype.Controller。
輸入org.springframework.ui.Model。
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.multipart.MultipartFile。

インポートのjavax.servlet.http.HttpServletResponse;
輸入 java.io. * ;
輸入java.util.UUID。

@Controller 
パブリック クラスUserControler { 

    @RequestMapping( "/アップロード" 公共の文字列のアップロード(ユーザーユーザー、MultipartFileファイル、モデルモデル){ 

        // 実際のファイル名 
        の文字列実名= file.getOriginalFilename(); 
        System.out.printlnは(file.getName()); 
        // 拡張 
        文字列の接尾辞=実名。ストリング(trueName.lastIndexOf( "" )); 

        文字列のパス = "D:/ IMG /" ; 
     // UUIDツールは、ファイルの一意のファイル名を生成する使用 文字列SAVENAME
= UUID.randomUUID()toString()を;. ファイルNEWFILE = 新しい新しいファイル(パスSAVENAME + + サフィックス); 試み{
      //ファイルが書き込ま file.transferTo(NEWFILE)。 }
キャッチ(IOExceptionをE){ e.printStackTrace(); } // 存储値 model.addAttribute( "パス"、経路+ SAVENAME + サフィックス)。 リターン「ログイン」; } @RequestMapping( "/ダウンロード" パブリック文字列ダウンロード(文字列名、HttpServletResponseのRESP){ resp.setContentType( "アプリケーション/オクテットストリーム" )。 resp.setCharacterEncoding( "UTF-8" ); // 设置文件的名字 resp.addHeader( "コンテンツ・処分"、 "添付ファイル;ファイル名=" InputStreamがあります =(例外e){; OutputStreamのOS = nullを試す{ ある = 新しいFileInputStreamを(名前); OS = resp.getOutputStream()。 バイト [] B = 新しい バイト [2048 * 10 ]。 int型のlen = 0 ; 一方、(!(LEN = is.read(B))= -1 ){ os.write(B、 0 、LEN)。 } os.flush()。 } キャッチ )(e.printStackTraceします。 } 最後に {         //关闭文件流 FileUtil.close(OSです)。 } 戻り値「成功」 } @RequestMapping( "/ログイン" パブリック文字列ログイン(){ 戻り "インデックス" } }

 

おすすめ

転載: www.cnblogs.com/qurui1997/p/11073632.html
おすすめ