久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

根據(jù)控制器更改文件大小限制 (maxUploadSize)

Changing file size limit (maxUploadSize) depending on the controller(根據(jù)控制器更改文件大小限制 (maxUploadSize))
本文介紹了根據(jù)控制器更改文件大小限制 (maxUploadSize)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè) Spring MVC 網(wǎng)絡(luò),它有兩個(gè)不同的頁(yè)面,它們有不同的表單來(lái)上傳不同的文件.其中一個(gè)應(yīng)該有 2MB 的限制,而另一個(gè)應(yīng)該有 50MB 的限制.

I have a Spring MVC web with two different pages that have different forms to upload different files. One of them should have a limitation of 2MB, while the other should have a 50MB limitation.

現(xiàn)在,我的 app-config.xml 中有這個(gè)限制:

Right now, I have this limitation in my app-config.xml:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes (2097152 B = 2 MB) -->
    <property name="maxUploadSize" value="2097152 "/>
</bean>

我可以像這樣在我的主控制器中解決 maxUploadSize 異常:

And I could resolve the maxUploadSize exception in my main controller like that:

@Override
public @ResponseBody
ModelAndView resolveException(HttpServletRequest arg0,
        HttpServletResponse arg1, Object arg2, Exception exception) {
    ModelAndView modelview = new ModelAndView();
        String errorMessage = "";
        if (exception instanceof MaxUploadSizeExceededException) {
            errorMessage =  String.format("El tama?o del fichero debe ser menor de  %s", UnitConverter.convertBytesToStringRepresentation(((MaxUploadSizeExceededException) exception)
                    .getMaxUploadSize()));

        } else {
            errorMessage = "Unexpected error: " + exception.getMessage();
        }
        saveError(arg0, errorMessage);
        modelview = new ModelAndView();
        modelview.setViewName("redirect:" + getRedirectUrl());
    }
    return modelview;
}

但這顯然只控制了 2MB 的限制.我怎么能限制 20MB 的呢?我試過(guò)這個(gè)解決方案:https://stackoverflow.com/a/11792952/1863783,它更新了限制運(yùn)行.但是,這會(huì)為每個(gè)會(huì)話和每個(gè)控制器更新它.因此,如果一個(gè)用戶正在為第一個(gè)表單上傳文件,另一個(gè)使用第二個(gè)表單上傳的用戶應(yīng)該有第一個(gè)的限制......

But this, obviously, only controlls the 2MB limit. How could I make the limitation for the 20MB one? I've tried this solution: https://stackoverflow.com/a/11792952/1863783, which updates the limitation in runtime. However, this updates it for every session and every controller. So, if one user is uploading a file for the first form, another using uploading in the second form should have the limitation of the first one...

有什么幫助嗎?謝謝

推薦答案

乍一看似乎不是最好的解決方案,但這取決于您的要求.

It may seem like not the best solution at first sight, but it depends on your requirements.

您可以為所有控制器設(shè)置一個(gè)具有最大上傳大小的全局選項(xiàng),并為特定控制器使用較小的值覆蓋它.

You can set a global option with maximum upload size for all controllers and override it with lower value for specific controllers.

application.properties 文件(Spring Boot)

application.properties file (Spring Boot)

spring.http.multipart.max-file-size=50MB

使用檢查上傳控制器

public void uploadFile(@RequestPart("file") MultipartFile file) {
    final long limit = 2 * 1024 * 1024;    // 2 MB
    if (file.getSize() > limit) {
        throw new MaxUploadSizeExceededException(limit);
    }
    StorageService.uploadFile(file);
}

如果文件大于 2MB,您將在日志中看到此異常:

In case of a file bigger than 2MB you'll get this exception in log:

org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 2097152 bytes exceeded

這篇關(guān)于根據(jù)控制器更改文件大小限制 (maxUploadSize)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 日韩av一区二区在线观看 | 精品国产一区二区三区成人影院 | 最新毛片网站 | 一级片av| 日本三级网址 | 在线观看国产91 | 国产精品久久777777 | 亚洲日韩中文字幕一区 | 中文一区二区 | 人操人人| 欧美一区二区三区视频 | 超碰成人免费 | 超碰在线国产 | 一区二区三区av | 91在线精品秘密一区二区 | 国产成人精品999在线观看 | 中日韩av | 国产一级电影在线 | 国产精品久久久久久久久久免费看 | 草草视频在线播放 | 高清亚洲 | 国产精品久久久久久久久免费 | 国产中文字幕在线观看 | 中文字幕精品一区久久久久 | 国产高清性xxxxxxxx | 久久33| 色综久久 | 欧美成人一级 | 免费一区在线 | 最新中文字幕在线 | 亚洲精品久久久久久下一站 | 欧美不卡一区 | 九九99九九精彩46 | a视频在线观看 | 精品香蕉一区二区三区 | 日韩中文一区二区三区 | 亚洲网视频 | 亚洲欧美日本在线 | 精品国产乱码久久久久久影片 | 黄a免费网络 | 99视频在线免费观看 |