<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    神奇好望角 The Magical Cape of Good Hope

    庸人不必自擾,智者何需千慮?
    posts - 26, comments - 50, trackbacks - 0, articles - 11
      BlogJava :: 首頁 ::  :: 聯(lián)系 :: 聚合  :: 管理
    在 Servlet 3.0 中處理 multipart/form-data 請求的兩個輔助方法 Two Assistant Methods for Dealing with multipart/form-data Request in Servlet 3.0
    Servlet 3.0 引入了 javax.servlet.http.Part 接口,從而提供了對 multipart/form-data 類型的 HTTP 請求的直接支持,我們從此就可以擺脫諸如 Apache Commons FileUpload 之類的第三方依賴。然而,該支持太過單純,所以還要多做點(diǎn)事情,以便能更有效地進(jìn)行工作。我將在本文中介紹兩個輔助方法。 Servlet 3.0 comes with out-of-the-box support for handling HTTP request of type multipart/form-data by introducing of javax.servlet.http .Part interface, freeing us from 3rd party dependencies such as Apache Commons FileUpload. However, the support so pure that we still need to do a little more work to get us work more effective. I'll demostrate two assistant methods in this article.
    首先,Servlet 3.0 沒有提供方法將 Part 對象專為字符串。multipart/form-data 請求也可能包含字符串參數(shù)。在這種情況下,我們可以寫一個工具方法來從 Part 對象讀取字節(jié),然后將其解析為字符串。為了方便,還可以讓這個方法能處理最常見的 application/x-www-form-urlencoded 請求: First, there's no method to translate a Part object to a string value in Servlet 3.0. A multipart/form-data request may also contain string parameters. In this case, we can wrtie a utility method to read bytes from the Part object and parse them to a string value. For convenience we also enable the method to handle the most common application/x-www-form-urlencoded request:
    1. /**
    2.  * Returns the value of a request parameter from the given {@code HttpServletRequest}
    3.  * as a {@code String}, or {@code null} if the parameter does not exist.
    4.  * This method can handle request of type both {@code application/x-www-form-urlencoded}
    5.  * and {@code multipart/form-data}.
    6.  * @param req The request object.
    7.  * @param name The name of the parameter.
    8.  * @param charset The name of charset for parsing bytes to string if the request
    9.  * if of type {@code multipart/form-data}. An improper charset might lead to
    10.  * messy code in the returned string.
    11.  * @return The velue of the parameter.
    12.  */
    13. public static String getParameter(HttpServletRequest req, String name,String charse) {
    14.     // First assume the request is of type application/x-www-form-urlencoded.
    15.     String value = req.getParameter(name);
    16.     if (value != null) {
    17.         return value;
    18.     }
    19.     // Trying to handle the request as a multipart/form-data type.
    20.     try {
    21.         Reader in = new InputStreamReader(part.getInputStream(), charset);
    22.         StringBuilder sb = new StringBuilder();
    23.         char[] buffer = new char[256];
    24.         int read = 0;
    25.         while ((read = in.read(buffer)) != -1) {
    26.             sb.append(buffer, 0, read);
    27.         }
    28.         in.close();
    29.         return sb.toString();
    30.     } catch (Exception ex) {
    31.         return null;
    32.     }
    33. }
    類似地,可以寫一個 getParameterValues 方法去處理名稱相同的多個參數(shù),所以就不在這里列出代碼了。第二個輔助方法用于從 Part 對象獲取文件名。Part 接口并沒有提供這種方法,但我們可以容易地從頭部提取出文件名: In the similar manner we can write a getParameterValues method to handle multiple parameter with the same name, so I'm not going to list the code here. The sencond assistant method is for getting the filename from a Part object. The Part interface doesn't provide such a method, but we can easily extract the filename from the header:
    1. private static final String FILENAME_KEY = "filename=\"";
    2. /**
    3.  * Gets the original filename of a {@code Part}. The filename is encoded in
    4.  * the {@code content-disposition} header of the {@code Part} as the value
    5.  * of {@code filename} parameter.
    6.  * @param part The {@code Part} object.
    7.  * @return The filename of the part, or {@code null} if the part doesn't
    8.  * contain a file.
    9.  */
    10. public static String getFileNameFromPart(Part part) {
    11.     String cdHeader = part.getHeader("content-disposition");
    12.     if (cdHeader == null) {
    13.         return null;
    14.     }
    15.     for (String s : cdHeader.split("; ")) {
    16.         if (s.startsWith(FILENAME_KEY)) {
    17.             // Some stupid browers, e.g. IE, put the full file path on the
    18.             // client machine as the value, so we have to extract the filename
    19.             // out from it.
    20.             String path = s.substring(FILENAME_KEY.length(),
    21.                     s.length() - 1).replaceAll("\\\\", "/");
    22.             return path.substring(path.lastIndexOf("/") + 1);
    23.         }
    24.     }
    25.     return null;
    26. }
    主站蜘蛛池模板: 日本免费网站观看| 一区二区三区无码视频免费福利| 国产1024精品视频专区免费 | 国产伦精品一区二区三区免费迷| 国产成人精品亚洲日本在线| 2021精品国产品免费观看| 日产亚洲一区二区三区| 精品无码AV无码免费专区| 亚洲精品在线播放| 精品国产免费一区二区三区| 国产精品亚洲一区二区无码| 特级淫片国产免费高清视频| 亚洲国产91在线| 日韩一区二区在线免费观看| 日韩欧美亚洲国产精品字幕久久久 | 亚洲人成色在线观看| 日韩成人在线免费视频| 国产成人综合亚洲一区| 亚洲精品一级无码鲁丝片| 免费91麻豆精品国产自产在线观看| 久久精品九九亚洲精品天堂| 久久国产高潮流白浆免费观看| 亚洲男人的天堂在线| 精品无码国产污污污免费| 午夜不卡AV免费| 亚洲国产精品无码久久久蜜芽 | 久久久久国色AV免费观看性色 | 国产亚洲高清不卡在线观看| 99久久久国产精品免费蜜臀| 亚洲乱码一二三四区国产| 日韩免费高清视频网站| av网站免费线看| 91嫩草亚洲精品| 免费在线观看视频a| 午夜无码A级毛片免费视频 | 亚洲中文无码a∨在线观看| 四虎在线视频免费观看| 亚洲午夜久久久久久噜噜噜| 最近免费字幕中文大全视频| 亚洲av午夜国产精品无码中文字 | 91丁香亚洲综合社区|