最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用SpringMVC接收文件流上傳和表單參數(shù)

 更新時(shí)間:2022年02月22日 16:47:25   作者:Zebe  
這篇文章主要介紹了使用SpringMVC接收文件流上傳和表單參數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

接收文件流上傳和表單參數(shù)

在SpringMVC中,接收文件流非常簡單,我們可以寫個(gè)接口用來接收一些文件,同時(shí)還可以接收表單參數(shù)。

代碼參考如下:

JAVA服務(wù)端代碼

/**
 * 接收文件流
 *
 * @param request 請求
 * @return OK
 */
@RequestMapping(value = "/receive/file", method = POST)
public String receiveFile(HttpServletRequest request) {
    // 轉(zhuǎn)換為 MultipartHttpServletRequest
    if (request instanceof MultipartHttpServletRequest) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 通過表單中的參數(shù)名來接收文件流(可用 file.getInputStream() 來接收輸入流)
        MultipartFile file = multipartRequest.getFile("file");
        System.out.println("上傳的文件名稱:" + file.getOriginalFilename());
        System.out.println("上傳的文件大小:" + file.getSize());
        // 接收其他表單參數(shù)
        String name = multipartRequest.getParameter("name");
        String content = multipartRequest.getParameter("content");
        System.out.println("name:" + name);
        System.out.println("content:" + content);
        return "OK";
    } else {
        return "不是 MultipartHttpServletRequest";
    }
}

HTML頁面代碼

<form action="http://127.0.0.1:8080/receive/file" method="post" enctype="multipart/form-data">
? ? <input type="file" name="file" id="file">
? ? <input type="text" name="content" value="內(nèi)容">
? ? <input type="text" name="name" value="名稱">
? ? <button type="submit">提交請求</button>
</form>

SpringMVC接收文件上傳,并對文件做處理

  • http client版本4.1.1
  • spring版本3.2.11

在這個(gè)例子里面我接收了文件,并轉(zhuǎn)發(fā)給另一個(gè)機(jī)器,其實(shí)接收的時(shí)候文件的流已經(jīng)拿到了,想保存到本地,或者對文件分析,自己可以斟酌。

springmvc配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com"/>
    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    <!-- 處理JSON -->
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <mvc:default-servlet-handler/>
    <!-- 日志輸出攔截器 -->
    <mvc:interceptors>
        <bean class="com.steward.interceptor.AccessInteceptor">
            <property name="accessLog">
                <value>true</value>
            </property>
        </bean>
    </mvc:interceptors>
    <!-- 解析器 -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html"/>
                <entry key="json" value="application/json"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <ref bean="viewResolver"/>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
            </list>
        </property>
    </bean>
    <!-- 異常處理 -->
    <bean id="exceptionHandler" class="com.steward.exception.ExceptionHandler"/>
    <!-- 文件上傳 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    <!-- 配置freeMarker的模板路徑 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
        <property name="suffix" value=".html"></property>
        <property name="contentType" value="text/html;charset=UTF-8"/>
        <property name="requestContextAttribute" value="rc"></property>
    </bean>
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/templates/"></property>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">0</prop>
                <prop key="default_encoding">UTF-8</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="number_format">0.##########</prop>
                <prop key="template_exception_handler">ignore</prop>
            </props>
        </property>
        <property name="freemarkerVariables">
            <map>
                <entry key="getVersion" value-ref="getVersion"/>
            </map>
        </property>
    </bean>
    <bean id="getVersion" class="com.steward.freemarker.GetVersion"/>
</beans>

controller代碼如下

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
@ResponseBody
public String uploadFile(HttpServletRequest request) throws Exception {
    FileOutputStream fos = null;
    InputStream in = null;
    String fileUrl = null;
    try {
        //將當(dāng)前上下文初始化給  CommonsMutipartResolver (多部分解析器)
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        //檢查form中是否有enctype="multipart/form-data"
        if (multipartResolver.isMultipart(request)) {
            //將request變成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            //獲取multiRequest 中所有的文件名
            Iterator iterator = multiRequest.getFileNames();
            while (iterator.hasNext()) {
                MultipartFile multipartFile = multiRequest.getFile(iterator.next().toString());
                in = multipartFile.getInputStream();
                File file = new File(multipartFile.getOriginalFilename());
                fos = new FileOutputStream(file);
                byte[] buff = new byte[1024];
                int len = 0;
                while ((len = in.read(buff)) > 0) {
                    fos.write(buff, 0, len);
                }
                String uploadUrl = platformHttpsDomain + "/uploadFile";
                HttpPost post = new HttpPost(uploadUrl);
                HttpClient httpclient = new DefaultHttpClient();
                MultipartEntity reqEntity = new MultipartEntity();
                reqEntity.addPart("file", new FileBody(file));
                post.setEntity(reqEntity);
                HttpResponse response = httpclient.execute(post);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    fileUrl = EntityUtils.toString(response.getEntity());
                }
                file.deleteOnExit();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }
    return fileUrl;
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java接口和抽象類實(shí)例分析

    Java接口和抽象類實(shí)例分析

    這篇文章主要介紹了Java接口和抽象類,實(shí)例分析了java接口與抽象類的概念與相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • Java中集合List、Set和Map的入門詳細(xì)介紹

    Java中集合List、Set和Map的入門詳細(xì)介紹

    Java集合主要分為三種類型:Set(集)、List(列表)和Map(映射),下面這篇文章主要給大家介紹了關(guān)于Java中集合List、Set和Map的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • 關(guān)于IDEA創(chuàng)建spark maven項(xiàng)目并連接遠(yuǎn)程spark集群問題

    關(guān)于IDEA創(chuàng)建spark maven項(xiàng)目并連接遠(yuǎn)程spark集群問題

    這篇文章主要介紹了IDEA創(chuàng)建spark maven項(xiàng)目并連接遠(yuǎn)程spark集群,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn)

    Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn)

    本文主要介紹了Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • java?安全?ysoserial?CommonsCollections6?分析

    java?安全?ysoserial?CommonsCollections6?分析

    這篇文章主要介紹了java?安全?ysoserial?CommonsCollections6示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 詳解ArrayBlockQueue源碼解析

    詳解ArrayBlockQueue源碼解析

    這篇文章主要介紹了ArrayBlockQueue源碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Java提示缺少返回語句的解決辦法

    Java提示缺少返回語句的解決辦法

    在本篇文章里小編給大家分享了關(guān)于Java提示缺少返回語句的解決辦法以及相關(guān)知識點(diǎn),需要的朋友們參考下。
    2019-07-07
  • Jackson的用法實(shí)例分析

    Jackson的用法實(shí)例分析

    這篇文章主要介紹了Jackson的用法實(shí)例分析,用于處理Java的json格式數(shù)據(jù)非常實(shí)用,需要的朋友可以參考下
    2014-08-08
  • Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼

    Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼

    本篇文章主要介紹了Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java使用百度AI接口實(shí)現(xiàn)智能機(jī)器人對話系統(tǒng)

    Java使用百度AI接口實(shí)現(xiàn)智能機(jī)器人對話系統(tǒng)

    AI已經(jīng)在各行各業(yè)中廣泛應(yīng)用,助力于各式各樣的業(yè)務(wù),而在機(jī)器人對話中,我們可以通過利用百度AI中的自然語言處理、問答知識圖譜等技術(shù),使機(jī)器人可以更加智能化、自然化的為用戶服務(wù),本文介紹Java利用百度AI接口實(shí)現(xiàn)智能機(jī)器人對話系統(tǒng)
    2024-01-01

最新評論

梅州市| 翼城县| 军事| 十堰市| 外汇| 邢台市| 汉源县| 蒙山县| 涿州市| 皮山县| 昌吉市| 军事| 延边| 西平县| 商河县| 新建县| 乐昌市| 禹城市| 湘阴县| 福安市| 界首市| 凌海市| 天气| 平乐县| 祁门县| 娱乐| 平远县| 宣化县| 恩施市| 南通市| 墨竹工卡县| 吴川市| 鹤岗市| 兴山县| 克拉玛依市| 锡林浩特市| 贵定县| 康平县| 郯城县| 红原县| 抚宁县|