通过js跳转url下载包含中文的文件乱码问题解决方案(java)

问题描述:

  通过js 跳转url的方式下载中文文件,因为中文文件名乱码找不到文件。

解决方案:

  经过测试在tomcat 8 及以上不会出现这个问题;

  以下解决方案博主亲测有效,如果您有更好的解决方案,请直接留言。互相进步。

如果是使用 tomcat7 在conf、server.xml中添加如下配置:

  

URIEncoding="UTF-8"

  配置完整:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 --><Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener"/>
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->

    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>

    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine defaultHost="localhost" name="Catalina">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>

      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log." suffix=".txt"/>

      <Context docBase="D:\apache-tomcat-7.0.82\webapps\platform" path="/platform" reloadable="true" source="org.eclipse.jst.jee.server:platform"/></Host>
    </Engine>
  </Service>
</Server>

原文地址:https://www.cnblogs.com/MrABlg/p/8119402.html

时间: 2024-10-10 12:02:04

通过js跳转url下载包含中文的文件乱码问题解决方案(java)的相关文章

[转]asp.net URL中包含中文参数造成乱码的解决方法

本文转自:http://www.jb51.net/article/22437.htm 问题: 前段时间,在系统中做了一个类似于友情链接的功能块,一直运行良好,直到有一天加了类似于以下的链接地址:http://www.****.com/user.aspx?id=水天,就出现大问题了: 1.从IE地址栏中直接输入这个地址,访问没错: 2.做一个静态页,其中包括这个超链接,点击访问也没错: 3.就是把这个链接添加到这个功能块中,点击访问那边接收到的是乱码. 一开始,被这个问题也搞得头大,在google

ElKstack-解决nginx日志url链接包含中文logstash报错问题

logstash报错现象 Trouble parsing json {:source=>"message", :raw=>"{\"@timestamp\":\"2016-05-30T14:51:27+08:00\",\"host\":\"10.139.48.166\",\"clientip\":\"180.109.110.203\",\"

windows下Python打开包含中文路径名文件

windows使用gbx(gb2312,gbk,gb18030我也不知道是哪个)对文件名及文件路径进行编码保存.打开文件的函数中使用诸如open(filename.encode('gbk'))可以很好的解决. #coding:utf8 if __name__ == '__main__': srcfile = r"D:/测试路径/测试文件.txt" f = open(srcfile.decode('utf8').encode('gbk')) for text in f.readlines

sublime text2 打开包含中文的文件会自动追加.dump后缀解决办法

用sublime text2 打开.c, .h,.txt等文件会自动追加一个.dump后缀,這样在打开.c,.h等文件时无法正常识别,从而无法正常进行语法着色,网上说是因为安装了GBK Encoding Support 插件的问题,于是就删除这个插件,发现再打开不会自动加.dump后缀了,但是遇到中文就乱码了, 因为GBK-.,这个插件就是解决中文乱码用的,那怎么办呢? 其实只要重新保存一下就可以了,比如我打开一个A.h文件,这个里有中文注释, 第一次打开时因为有中文所以sublime text

Linux系统中 Sublime Text 中文 GBK 文件乱码问题

Sublime Text 是一个很不错编辑器,具有漂亮的界面和强大的功能.再加上丰富的插件,而且还跨平台,绝对是一款实打实的神器啊! 众所周知,Sublime Text 对中文支持的极差,可以说几乎就没有特别的支持.尤其是 GBK 编码的中文文件,直接打开就是一团乱码.ST 的开发者 Jon Skinner 貌似对中国市场不怎么感冒,一直未加中文 GBK 的支持.既然开发者不给支持中文,那么就只能靠中国用户自己解决问题了.在这里感谢热心网友 seanliang 开发了强大的 ConvertToU

txt excel 导出 文件名称为中文,避免乱码的解决方案

在通过response导出文件数据的时候,不论是txt 还是Excel ,如果想让其文件名称为中文,解决方案: response.setCharacterEncoding("UTF-8"); response.setContentType("application/txt");//"application/vnd.ms-excel" response.setHeader("Content-disposition","

Extjs4.2 ajax请求url中传中文參数乱码问题

今天有个需求须要在url中传入中文參数.结果在后台取得时出现乱码,怀疑可能是编码问题.上网查询了资料,试了几种办法.发现有一种可行,记录在此,以便查阅. url中用encodeURI 进行2次编码: Ext.Ajax.request({ url:"updateadminuser.do?"+userid+"&uname="+encodeURI(encodeURI(uname)), success:function (response) { store.load

python中包含中文list输出乱码

先看代码: item = [['2015',u'中国'],['2013','nian份']] print item print item[0][1] 输出结果: 在此处有相关讨论 解决办法: python2中,list若包含中文,整体输出时是以十六进制输出的: 链接中的讨论提供的方法试过,没成功.看来python2中只能以for来单独输出了. 当然了python3应该是解决了此类问题的.

js获取当前url地址参数中文乱码问题

网上看了一些关于此问题的文章,都说的不清不楚,有些更是乱七八糟,完全没法看,故此找了一篇能用的,借鉴作为笔记. //首先获取到当前页面的地址栏信息 var url = window.location.href;//获取url地址 var obj = {};//待会用来存放参数的对象 var reg = /\?/;匹配从?截取 if(url.match(reg)) { //判断传入参数,以问号截取,问号后是参数 var chars = url.split('?')[1]; var arr = ch