{"id":9070,"date":"2013-10-25T19:30:06","date_gmt":"2013-10-25T10:30:06","guid":{"rendered":"http:\/\/arison.jp\/blog\/?p=9070"},"modified":"2016-03-30T12:01:29","modified_gmt":"2016-03-30T03:01:29","slug":"fileitem-convert-utf8-2-sjis","status":"publish","type":"post","link":"https:\/\/arison.jp\/blog\/2013\/10\/25\/fileitem-convert-utf8-2-sjis\/","title":{"rendered":"\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u305f\u30d5\u30a1\u200b\u30a4\u30eb\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u5909\u63db\u200b\u65b9\u6cd5(SJIS\u304b\u3089UTF8)"},"content":{"rendered":"<p>\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u305f\u30d5\u30a1\u200b\u30a4\u30eb\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u5909\u63db\u200b\u65b9\u6cd5(SJIS\u304b\u3089UTF8)<\/p>\n<p>Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067<span class=\"font24px\">Shift-JIS\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3066\u5185\u90e8\u3067UTF-8\u306b\u5909\u63db\u3057\u3066\u51fa\u529b\u3059\u308b\u30b5\u30f3\u30d7\u30eb<\/span>\u3092\u4f5c\u3063\u3066\u307f\u305f\u3002<\/p>\n<p>\u3042\u304f\u307e\u3067\u53c2\u8003\u7a0b\u5ea6\u3067\u304a\u9858\u3044\u3044\u305f\u3057\u307e\u3059\u3002<br \/>\n\u7c21\u5358\u306b\u52d5\u4f5c\u78ba\u8a8d\u307e\u3067\u5b9f\u65bd\u3057\u3066\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea<br \/>\ncommons-fileupload-1.3-bin.zip<br \/>\ncommons-io-2.4-bin.zip<\/p>\n<p>\u52d5\u7684\u30b3\u30f3\u30c6\u30f3\u30c4\u3067\u4f5c\u6210\u3002<\/p>\n<p>UploadFileServlet.java<br \/>\n[java]<\/p>\n<p>import java.io.FileOutputStream;<br \/>\nimport java.io.IOException;<br \/>\nimport java.io.InputStreamReader;<br \/>\nimport java.io.OutputStreamWriter;<br \/>\nimport java.util.Iterator;<br \/>\nimport java.util.List;<\/p>\n<p>import javax.servlet.http.HttpServlet;<br \/>\nimport javax.servlet.http.HttpServletRequest;<br \/>\nimport javax.servlet.http.HttpServletResponse;<\/p>\n<p>import org.apache.commons.fileupload.FileItem;<br \/>\nimport org.apache.commons.fileupload.FileUploadException;<br \/>\nimport org.apache.commons.fileupload.disk.DiskFileItemFactory;<br \/>\nimport org.apache.commons.fileupload.servlet.ServletFileUpload;<\/p>\n<p>public class UploadFileServlet extends HttpServlet {<\/p>\n<p>    private String outPutPath = &quot;C:\/tmp\/output.txt&quot;;<br \/>\n    private String redirectPage = &quot;upload.html&quot;;<\/p>\n<p>    private String inEncoding = &quot;MS932&quot;;<br \/>\n    private String outEncoding = &quot;UTF-8&quot;;<\/p>\n<p>    \/**<br \/>\n     * \u6587\u5b57\u30b3\u30fc\u30c9\u5909\u63db\u3057\u3066\u66f8\u304d\u8fbc\u3080\u4f8b<br \/>\n     *\/<br \/>\n    public void doPost(HttpServletRequest req,<br \/>\n        HttpServletResponse res) throws IOException {<\/p>\n<p>        \/\/ Shift_JIS\u306e\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u6587\u5b57\u30b3\u30fc\u30c9\u5909\u63db\u3057\u3066\u66f8\u304d\u8fbc\u3080\u4f8b<\/p>\n<p>        DiskFileItemFactory factory = new DiskFileItemFactory();<br \/>\n        ServletFileUpload upload = new ServletFileUpload(factory);<\/p>\n<p>        factory.setSizeThreshold(1024);<br \/>\n        upload.setSizeMax(-1);<br \/>\n        upload.setHeaderEncoding(inEncoding);<\/p>\n<p>        InputStreamReader in = null;<\/p>\n<p>        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(outPutPath), outEncoding);<\/p>\n<p>        try {<br \/>\n            List&lt;FileItem&gt; list = upload.parseRequest(req);<\/p>\n<p>            Iterator&lt;FileItem&gt; iterator = list.iterator();<br \/>\n            while (iterator.hasNext()) {<br \/>\n                FileItem fItem = iterator.next();<\/p>\n<p>                if (!(fItem.isFormField())) {<\/p>\n<p>                    String fileName = fItem.getName();<br \/>\n                    if ((fileName != null) &amp;&amp; (!fileName.equals(&quot;&quot;))) {<br \/>\n                        in = new InputStreamReader(fItem.getInputStream(), inEncoding);<br \/>\n                        int cbuf;<br \/>\n                        while ((cbuf = in.read()) &gt; -1) {<br \/>\n                            System.out.println(cbuf);<br \/>\n                            out.write(cbuf);<br \/>\n                        }<br \/>\n                        out.flush();<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n        } catch (FileUploadException e) {<br \/>\n            e.printStackTrace();<br \/>\n        } catch (Exception e) {<br \/>\n            e.printStackTrace();<br \/>\n        } finally {<br \/>\n            if (out != null) {<br \/>\n                try {<br \/>\n                    out.close();<br \/>\n                } catch (IOException e) {<br \/>\n                    \/\/ ignore<br \/>\n                }<br \/>\n            }<br \/>\n            if (in != null) {<br \/>\n                try {<br \/>\n                    in.close();<br \/>\n                } catch (IOException e) {<br \/>\n                    \/\/ ignore<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        res.sendRedirect(redirectPage);<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>upload.html<br \/>\n[html]<br \/>\n&lt;html&gt;<br \/>\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=UTF-8&quot;&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;form method=&quot;POST&quot; enctype=&quot;multipart\/form-data&quot; action=&quot;upload&quot;&gt;<br \/>\n  \u30d5\u30a1\u30a4\u30eb\u30a2\u30c3\u30d7\u30ed\u30fc\u30c91: &lt;input type=&quot;file&quot; name=&quot;upfile1&quot;&gt;&lt;br\/&gt;<br \/>\n  \u30d5\u30a1\u30a4\u30eb\u30a2\u30c3\u30d7\u30ed\u30fc\u30c92: &lt;input type=&quot;file&quot; name=&quot;upfile2&quot;&gt;&lt;br\/&gt;<br \/>\n  &lt;input type=&quot;submit&quot; value=&quot;\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9&quot;&gt; \u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u306e\u30c6\u30b9\u30c8<br \/>\n&lt;\/form&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n[\/html]<\/p>\n<p>web.xml<br \/>\n[xml]<br \/>\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br \/>\n&lt;web-app id=&quot;WebTest&quot; version=&quot;2.4&quot; xmlns=&quot;http:\/\/java.sun.com\/xml\/ns\/j2ee&quot;<br \/>\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;<br \/>\n\txsi:schemaLocation=&quot;http:\/\/java.sun.com\/xml\/ns\/j2ee http:\/\/java.sun.com\/xml\/ns\/j2ee\/web-app_2_4.xsd&quot;&gt;<\/p>\n<p>\t&lt;display-name&gt;WebTest&lt;\/display-name&gt;<br \/>\n\t&lt;servlet&gt;<br \/>\n\t\t&lt;servlet-name&gt;upload&lt;\/servlet-name&gt;<br \/>\n\t\t&lt;servlet-class&gt;UploadFileServlet&lt;\/servlet-class&gt;<br \/>\n\t&lt;\/servlet&gt;<br \/>\n\t&lt;servlet-mapping&gt;<br \/>\n\t\t&lt;servlet-name&gt;upload&lt;\/servlet-name&gt;<br \/>\n\t\t&lt;url-pattern&gt;\/upload&lt;\/url-pattern&gt;<br \/>\n\t&lt;\/servlet-mapping&gt;<br \/>\n&lt;\/web-app&gt;<br \/>\n[\/xml]<\/p>\n<p>WEB-INF\u306elib\u306e\u914d\u4e0b\u306b<br \/>\ncommons-fileupload-1.3-bin.zip<br \/>\ncommons-io-2.4-bin.zip<br \/>\n\u914d\u7f6e\u3057\u3066\u3002<\/p>\n<p>\u5f8c\u306e\u3081\u3093\u3069\u3044\u8a2d\u5b9a\u5468\u308a\u306f\u9069\u5f53\u306b\u3057\u3066\u3002<\/p>\n<p>\u30c7\u30d7\u30ed\u30a4\u3057\u3066\u3001\u30b5\u30fc\u30d0\u30fc\u8d77\u52d5\u3057\u3066<br \/>\nhttp:\/\/localhost:8080\/WebTest\/upload.html \u306b\u30a2\u30af\u30bb\u30b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u305f\u30d5\u30a1\u200b\u30a4\u30eb\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u5909\u63db\u200b\u65b9\u6cd5(SJIS\u304b\u3089UTF8) Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067Shift-JIS\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3066\u5185\u90e8\u3067UTF-8\u306b\u5909\u63db\u3057\u3066\u51fa\u529b\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u3092\u4f5c\u3063\u3066\u307f\u305f\u3002 \u3042\u304f\u307e\u3067\u53c2\u8003\u7a0b\u5ea6\u3067 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":17178,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[703,63],"class_list":["post-9070","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-java","tag-web"],"_links":{"self":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts\/9070","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/comments?post=9070"}],"version-history":[{"count":0,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts\/9070\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/media\/17178"}],"wp:attachment":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/media?parent=9070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/categories?post=9070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/tags?post=9070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}