`
Odysseus_110
  • 浏览: 121523 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

struts 文件上传 乱码

阅读更多

action 代码:

 

 

	public void savemovieinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		
		MovieInfoForm mif = (MovieInfoForm)form;
		// Check that we have a file upload request
		try {
		 FormFile file = mif.getFile();
		 InputStream moviefile_ = file.getInputStream();
	         String fileName_ = file.getFileName();
			 
	         String intro = mif.getIntro();
	       
			 
		 FtpClientCommonNetImpl client_ = new FtpClientCommonNetImpl();
		 client_.setIJTH(jth_);
			 
		 Properties config_ = new Properties();
		 config_.setProperty("server", sysConfigForm.getFtp_movie_ip());
		 config_.setProperty("username", sysConfigForm.getFtp_movie_username());
		 config_.setProperty("password", sysConfigForm.getFtp_movie_psd());
		 client_.setConfig(config_);
		 client_.upload(moviefile_, movienamesys, true);
			 
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 

 

FtpClientCommonNetImpl具体操作类代码见上一篇网页控件 上传 文件

 

利用strtus进行文件上传与通过servlet进行文件上传的区别同样在于获取客户端文件的方式,由于strtus对页面的Form进行了封装,所以我们只能通过struts的ActionForm来获取文件流。

 

servlet中得到文件流是通过fileupload 包的FileItem:

 

List /* FileItem */ items = upload.parseRequest(request);
FileItem item = (FileItem) iter.next();
String fileName = item.getName();
InputStream localIn = new FileInputStream(fileName);

 

 用这种方式上传文件时,中文文件名称是没有问题的,要将文件名称进行转码,new String(paravalue.getBytes("GBK"),"ISO8859-1") 就不会有问题。

 

而通过struts来上传文件时用到的是FormFile类,

 

 

MovieInfoForm mif = (MovieInfoForm)form;
// Check that we have a file upload request
			
FormFile file = mif.getFile();
String intro = mif.getIntro();
String fileName_ = file.getFileName();
	         
InputStream localIn_ = file.getInputStream();

 但用file.getFileName()方法得到的文件名称是乱码,(页面编码是”UTF-8”),跟踪调试发现从struts的ActionForm 中取得的intro转一下码是正常的( new String(paravalue.getBytes("GBK"),"ISO8859-1") ),所以可能是struts封装的问题,google一下:http://hi.baidu.com/yds_amber/blog/item/4cdb0601451a8c05738da58f.html

 

 

所以这里必须用controller来处理,但spring配置文件里已经配置了 org.springframework.web.struts.DelegatingRequestProcessor 

 

 

<controller>
		<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>

 

 不知道这里能不能配置多个property属性,自己试了试没成功,所以这里只能配置成EncodingProcessor

 

 

   <controller>
		<!--  set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/-->
		<set-property property="processorClass" value="com.business.admin.processor.EncodingProcessor"/>
   </controller>

 

 EncodingProcessor类:

 

 

public class EncodingProcessor extends RequestProcessor{

	public void process(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
	   String encoding="UTF-8";
	   request.setCharacterEncoding(encoding);
	   response.setContentType("text/html;charset=UTF-8");
	   super.process(request, response);
	}
}
 

 

但这样去掉DelegatingRequestProcessor配置就不能让spring来代理处理请求了,查看DelegatingRequestProcessor 源代码:

 

 

public class org.springframework.web.struts.DelegatingRequestProcessor extends org.apache.struts.action.RequestProcessor {

 

 

这个类已经继承了struts的RequestProcess类,所以修改一下以上代码,让EncodingProcessor 继承 DelegatingRequestProcessor就好了:

 

 

public class EncodingProcessor extends DelegatingRequestProcessor{

	public void process(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
	   String encoding="UTF-8";
	   request.setCharacterEncoding(encoding);
	   response.setContentType("text/html;charset=UTF-8");
	   super.process(request, response);
	}
}
 

 

 

重启后,发现从request String fileName_ = file.getFileName(); 得到的值不用转码也是正常的。但上传中文文件名时候还是报错:

 

 

 

java.lang.Exception: java.lang.Exception:  文件上传失败! 
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
	at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
	at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
	at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
	at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.Exception:  文件上传失败! 
	at com.util.ftp.FtpClientCommonNetImpl.upload(FtpClientCommonNetImpl.java:116)
	... 30 more
 

 

 

这个问题折腾了两天,最后发现实页面编码设置和EncodingProcessor 里的设置不一样,将页面里的编码也设置成 UTF-8 就好了

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

 

 

后来又不知道动了哪根筋,只要上传中文名称的文件就还是报上面的错,debug里看从ActionForm 里取到的信息都是正常的中文字符。于是在ftp类里加了

 

 

       FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
       conf.setServerLanguageCode("zh");
       ftp.setControlEncoding("UTF-8");

 

 

就可以上传客户端的中文字符的文件了,但是上传时成功后,到ftp服务器上的文件都成乱码了。于是怀疑conf.setServerLanguageCode("zh");没起作用,查看源代码:

 

/*
 * 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.
 */

package org.apache.commons.net.ftp;

import java.text.DateFormatSymbols;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

/**
 * <p>
 * This class implements an alternate means of configuring the
 * {@link  org.apache.commons.net.ftp.FTPClient  FTPClient} object and
 * also subordinate objects which it uses.  Any class implementing the 
 * {@link  org.apache.commons.net.ftp.Configurable  Configurable } 
 * interface can be configured by this object. 
 * </p><p>
 * In particular this class was designed primarily to support configuration
 * of FTP servers which express file timestamps in formats and languages 
 * other than those for the US locale, which although it is the most common
 * is not universal.  Unfortunately, nothing in the FTP spec allows this to 
 * be determined in an automated way, so manual configuration such as this
 * is necessary.
 * </p><p>
 * This functionality was designed to allow existing clients to work exactly
 * as before without requiring use of this component.  This component should
 * only need to be explicitly invoked by the user of this package for problem
 * cases that previous implementations could not solve.
 * </p>
 * <h3>Examples of use of FTPClientConfig</h3>
 * Use cases:
 * You are trying to access a server that 
 * <ul> 
 * <li>lists files with timestamps that use month names in languages other 
 * than English</li>
 * <li>lists files with timestamps that use date formats other 
 * than the American English "standard" <code>MM dd yyyy</code></li>
 * <li>is in different timezone and you need accurate timestamps for 
 * dependency checking as in Ant</li>
 * </ul>
 * <p>
 * Unpaged (whole list) access on a UNIX server that uses French month names
 * but uses the "standard" <code>MMM d yyyy</code> date formatting
 * <pre>
 *    FTPClient f=FTPClient();
 *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
 *    conf.setServerLanguageCode("fr");
 *    f.configure(conf);
 *    f.connect(server);
 *    f.login(username, password);
 *    FTPFile[] files = listFiles(directory);
 * </pre>
 * </p>
 * <p>
 * Paged access on a UNIX server that uses Danish month names
 * and "European" date formatting in Denmark's time zone, when you
 * are in some other time zone.
 * <pre>
 *    FTPClient f=FTPClient();
 *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
 *    conf.setServerLanguageCode("da");
 *    conf.setDefaultDateFormat("d MMM yyyy");
 *    conf.setRecentDateFormat("d MMM HH:mm");
 *    conf.setTimeZoneId("Europe/Copenhagen");
 *    f.configure(conf);
 *    f.connect(server);
 *    f.login(username, password);
 *    FTPListParseEngine engine =
 *       f.initiateListParsing("com.whatever.YourOwnParser", directory);
 *
 *    while (engine.hasNext()) {
 *       FTPFile[] files = engine.getNext(25);  // "page size" you want
 *       //do whatever you want with these files, display them, etc.
 *       //expensive FTPFile objects not created until needed.
 *    }
 * </pre>
 * </p> 
 * <p>
 * Unpaged (whole list) access on a VMS server that uses month names
 * in a language not {@link #getSupportedLanguageCodes() supported} by the system.
 * but uses the "standard" <code>MMM d yyyy</code> date formatting
 * <pre>
 *    FTPClient f=FTPClient();
 *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_VMS);
 *    conf.setShortMonthNames(
 *        "jan|feb|mar|apr|ma\u00ED|j\u00FAn|j\u00FAl|\u00e1g\u00FA|sep|okt|n\u00F3v|des");
 *    f.configure(conf);
 *    f.connect(server);
 *    f.login(username, password);
 *    FTPFile[] files = listFiles(directory);
 * </pre>
 * </p>
 * <p>
 * Unpaged (whole list) access on a Windows-NT server in a different time zone.
 * (Note, since the NT Format uses numeric date formatting, language issues
 * are irrelevant here).
 * <pre>
 *    FTPClient f=FTPClient();
 *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
 *    conf.setTimeZoneId("America/Denver");
 *    f.configure(conf);
 *    f.connect(server);
 *    f.login(username, password);
 *    FTPFile[] files = listFiles(directory);
 * </pre>
 * </p>
 * Unpaged (whole list) access on a Windows-NT server in a different time zone
 * but which has been configured to use a unix-style listing format.
 * <pre>
 *    FTPClient f=FTPClient();
 *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
 *    conf.setTimeZoneId("America/Denver");
 *    f.configure(conf);
 *    f.connect(server);
 *    f.login(username, password);
 *    FTPFile[] files = listFiles(directory);
 * </pre>
 * </p>
 * @since 1.4
 * @see org.apache.commons.net.ftp.Configurable
 * @see org.apache.commons.net.ftp.FTPClient
 * @see org.apache.commons.net.ftp.parser.FTPTimestampParserImpl#configure(FTPClientConfig)
 * @see org.apache.commons.net.ftp.parser.ConfigurableFTPFileEntryParserImpl
 */
public class FTPClientConfig
{
    
    /**
     * Identifier by which a unix-based ftp server is known throughout
     * the commons-net ftp system.
     */
    public static final String SYST_UNIX  = "UNIX";

    /**
     * Identifier by which a vms-based ftp server is known throughout
     * the commons-net ftp system.
     */
    public static final String SYST_VMS   = "VMS";
    
    /**
     * Identifier by which a WindowsNT-based ftp server is known throughout
     * the commons-net ftp system.
     */
    public static final String SYST_NT    = "WINDOWS";

    /**
     * Identifier by which an OS/2-based ftp server is known throughout
     * the commons-net ftp system.
     */
    public static final String SYST_OS2   = "OS/2";

    /**
     * Identifier by which an OS/400-based ftp server is known throughout
     * the commons-net ftp system.
     */
    public static final String SYST_OS400 = "OS/400";
    
    /**
     * Identifier by which an AS/400-based ftp server is known throughout
     * the commons-net ftp system.
     */
    public static final String SYST_AS400 = "AS/400";
    
    /**
     * Identifier by which an MVS-based ftp server is known throughout
     * the commons-net ftp system.
     */
    public static final String SYST_MVS = "MVS";

    /**
     * Some servers return an "UNKNOWN Type: L8" message
     * in response to the SYST command. We set these to be a Unix-type system.
     * This may happen if the ftpd in question was compiled without system
     * information.
     *
     * NET-230 - Updated to be UPPERCASE so that the check done in
     * createFileEntryParser will succeed.
     *
     * @since 1.5
     */
    public static final String SYST_L8 = "TYPE: L8";
    
    /**
     * Identifier by which an Netware-based ftp server is known throughout
     * the commons-net ftp system.
     *
     * @since 1.5
     */
    public static final String SYST_NETWARE = "NETWARE";
    
    private final String serverSystemKey;
    private String defaultDateFormatStr = null;
    private String recentDateFormatStr = null;
    private boolean lenientFutureDates = false;
    private String serverLanguageCode = null;
    private String shortMonthNames = null;
    private String serverTimeZoneId = null;
    
    
    /**
     * The main constructor for an FTPClientConfig object
     * @param systemKey key representing system type of the  server being 
     * connected to. See {@link #getServerSystemKey() serverSystemKey}
     */
    public FTPClientConfig(String systemKey) {
        this.serverSystemKey = systemKey;
    }

    /**
     * Convenience constructor mainly for use in testing.
     * Constructs a UNIX configuration. 
     */
    public FTPClientConfig() {
        this(SYST_UNIX);
    }

    /**
     * Constructor which allows setting of all member fields
     * @param systemKey key representing system type of the  server being 
     * connected to. See 
     *  {@link #getServerSystemKey() serverSystemKey}
     * @param defaultDateFormatStr See 
     *  {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr}
     * @param recentDateFormatStr See
     *  {@link  #setRecentDateFormatStr(String)  recentDateFormatStr}
     * @param serverLanguageCode See
     *  {@link  #setServerLanguageCode(String)  serverLanguageCode}
     * @param shortMonthNames See
     *  {@link  #setShortMonthNames(String)  shortMonthNames}
     * @param serverTimeZoneId See
     *  {@link  #setServerTimeZoneId(String)  serverTimeZoneId}
     */
    public FTPClientConfig(String systemKey,
                           String defaultDateFormatStr,
                           String recentDateFormatStr,
                           String serverLanguageCode,
                           String shortMonthNames,
                           String serverTimeZoneId)
    {
        this(systemKey);
        this.defaultDateFormatStr = defaultDateFormatStr;
        this.recentDateFormatStr = recentDateFormatStr;
        this.serverLanguageCode = serverLanguageCode;
        this.shortMonthNames = shortMonthNames;
        this.serverTimeZoneId = serverTimeZoneId;
    }
    
    private static Map<String, Object> LANGUAGE_CODE_MAP = new TreeMap<String, Object>();
    static {
        
        // if there are other commonly used month name encodings which
        // correspond to particular locales, please add them here.
        
        
        
        // many locales code short names for months as all three letters
        // these we handle simply.
        LANGUAGE_CODE_MAP.put("en", Locale.ENGLISH);
        LANGUAGE_CODE_MAP.put("de",Locale.GERMAN);
        LANGUAGE_CODE_MAP.put("it",Locale.ITALIAN);
        LANGUAGE_CODE_MAP.put("es", new Locale("es", "", "")); // spanish
        LANGUAGE_CODE_MAP.put("pt", new Locale("pt", "", "")); // portuguese
        LANGUAGE_CODE_MAP.put("da", new Locale("da", "", "")); // danish
        LANGUAGE_CODE_MAP.put("sv", new Locale("sv", "", "")); // swedish
        LANGUAGE_CODE_MAP.put("no", new Locale("no", "", "")); // norwegian
        LANGUAGE_CODE_MAP.put("nl", new Locale("nl", "", "")); // dutch
        LANGUAGE_CODE_MAP.put("ro", new Locale("ro", "", "")); // romanian
        LANGUAGE_CODE_MAP.put("sq", new Locale("sq", "", "")); // albanian
        LANGUAGE_CODE_MAP.put("sh", new Locale("sh", "", "")); // serbo-croatian
        LANGUAGE_CODE_MAP.put("sk", new Locale("sk", "", "")); // slovak
        LANGUAGE_CODE_MAP.put("sl", new Locale("sl", "", "")); // slovenian


        // some don't
        LANGUAGE_CODE_MAP.put("fr",
                "jan|f\u00e9v|mar|avr|mai|jun|jui|ao\u00fb|sep|oct|nov|d\u00e9c");  //french
            
    }
    
    /**
     * Getter for the serverSystemKey property.  This property
     * specifies the general type of server to which the client connects.
     * Should be either one of the <code>FTPClientConfig.SYST_*</code> codes
     * or else the fully qualified class name of a parser implementing both
     * the <code>FTPFileEntryParser</code> and <code>Configurable</code>
     * interfaces.
     * @return Returns the serverSystemKey property.
     */
    public String getServerSystemKey() {
        return serverSystemKey;
    }
    
    /**
     * getter for the {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr} 
     * property.  
     * @return Returns the defaultDateFormatStr property.
     */
    public String getDefaultDateFormatStr() {
        return defaultDateFormatStr;
    }
    
    /**
     * getter for the {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} property.
     * @return Returns the recentDateFormatStr property.
     */

    public String getRecentDateFormatStr() {
        return recentDateFormatStr;
    }
    
    /**
     * getter for the {@link  #setServerTimeZoneId(String)  serverTimeZoneId} property.
     * @return Returns the serverTimeZoneId property.
     */
    public String getServerTimeZoneId() {
        return serverTimeZoneId;
    }
    
    /**
     * <p>
     * getter for the {@link  #setShortMonthNames(String)  shortMonthNames} 
     * property.  
     * </p>
     * @return Returns the shortMonthNames.
     */
    public String getShortMonthNames() {
        return shortMonthNames;
    }
    
    /**
     * <p>
     * getter for the {@link  #setServerLanguageCode(String)  serverLanguageCode} property.
     * </p>  
     * @return Returns the serverLanguageCode property.
     */
    public String getServerLanguageCode() {
        return serverLanguageCode;
    }
    
    /**
     * <p>
     * getter for the {@link  #setLenientFutureDates(boolean)  lenientFutureDates} property.
     * </p>  
     * @return Returns the lenientFutureDates.
     * @since 1.5
     */
    public boolean isLenientFutureDates() {
        return lenientFutureDates;
    }
    /**
     * <p>
     * setter for the defaultDateFormatStr property.  This property
     * specifies the main date format that will be used by a parser configured
     * by this configuration to parse file timestamps.  If this is not
     * specified, such a parser will use as a default value, the most commonly
     * used format which will be in as used in <code>en_US</code> locales.
     * </p><p>
     * This should be in the format described for 
     * <code>java.text.SimpleDateFormat</code>. 
     * property.
     * </p>
     * @param defaultDateFormatStr The defaultDateFormatStr to set.
     */
    public void setDefaultDateFormatStr(String defaultDateFormatStr) {
        this.defaultDateFormatStr = defaultDateFormatStr;
    }
    
    /**
     * <p>
     * setter for the recentDateFormatStr property.  This property
     * specifies a secondary date format that will be used by a parser 
     * configured by this configuration to parse file timestamps, typically 
     * those less than a year old.  If this is  not specified, such a parser 
     * will not attempt to parse using an alternate format.
     * </p>
     * This is used primarily in unix-based systems.
     * </p>
     * This should be in the format described for 
     * <code>java.text.SimpleDateFormat</code>.
     * </p>
     * @param recentDateFormatStr The recentDateFormatStr to set.
     */
    public void setRecentDateFormatStr(String recentDateFormatStr) {
        this.recentDateFormatStr = recentDateFormatStr;
    }
    
    /**
     * <p>
     * setter for the lenientFutureDates property.  This boolean property
     * (default: false) only has meaning when a 
     * {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} property
     * has been set.  In that case, if this property is set true, then the
     * parser, when it encounters a listing parseable with the recent date 
     * format, will only consider a date to belong to the previous year if
     * it is more than one day in the future.  This will allow all 
     * out-of-synch situations (whether based on "slop" - i.e. servers simply 
     * out of synch with one another or because of time zone differences - 
     * but in the latter case it is highly recommended to use the 
     * {@link  #setServerTimeZoneId(String)  serverTimeZoneId} property
     * instead) to resolve correctly.
     * </p><p>
     * This is used primarily in unix-based systems.
     * </p>
     * @param lenientFutureDates set true to compensate for out-of-synch 
     * conditions.
     */
    public void setLenientFutureDates(boolean lenientFutureDates) {
        this.lenientFutureDates = lenientFutureDates;
    }
    /**
     * <p>
     * setter for the serverTimeZoneId property.  This property
     * allows a time zone to be specified corresponding to that known to be 
     * used by an FTP server in file listings.  This might be particularly 
     * useful to clients such as Ant that try to use these timestamps for 
     * dependency checking.
     * </p><p>
     * This should be one of the identifiers used by 
     * <code>java.util.TimeZone</code> to refer to time zones, for example, 
     * <code>America/Chicago</code> or <code>Asia/Rangoon</code>.
     * </p>
     * @param serverTimeZoneId The serverTimeZoneId to set.
     */
    public void setServerTimeZoneId(String serverTimeZoneId) {
        this.serverTimeZoneId = serverTimeZoneId;
    }
    
    /**
     * <p>
     * setter for the shortMonthNames property.  
     * This property allows the user to specify a set of month names
     * used by the server that is different from those that may be 
     * specified using the {@link  #setServerLanguageCode(String)  serverLanguageCode}
     * property.
     * </p><p>
     * This should be a string containing twelve strings each composed of
     * three characters, delimited by pipe (|) characters.  Currently, 
     * only 8-bit ASCII characters are known to be supported.  For example,
     * a set of month names used by a hypothetical Icelandic FTP server might 
     * conceivably be specified as 
     * <code>"jan|feb|mar|apr|ma&#xED;|j&#xFA;n|j&#xFA;l|&#xE1;g&#xFA;|sep|okt|n&#xF3;v|des"</code>.  
     * </p>
     * @param shortMonthNames The value to set to the shortMonthNames property.
     */
    public void setShortMonthNames(String shortMonthNames) {
        this.shortMonthNames = shortMonthNames;
    }
    
    /**
     * <p>
     * setter for the serverLanguageCode property.  This property allows
     * user to specify a 
     * <a href="http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt">
     * two-letter ISO-639 language code</a> that will be used to 
     * configure the set of month names used by the file timestamp parser.
     * If neither this nor the {@link #setShortMonthNames(String) shortMonthNames} 
     * is specified, parsing will assume English month names, which may or 
     * may not be significant, depending on whether the date format(s) 
     * specified via {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr} 
     * and/or {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} are using 
     * numeric or alphabetic month names.
     * </p>
     * <p>If the code supplied is not supported here, <code>en_US</code>
     * month names will be used.  We are supporting here those language 
     * codes which, when a <code> java.util.Locale</code> is constucted
     * using it, and a <code>java.text.SimpleDateFormat</code> is 
     * constructed using that Locale, the array returned by the 
     * SimpleDateFormat's <code>getShortMonths()</code> method consists
     * solely of three 8-bit ASCII character strings.  Additionally, 
     * languages which do not meet this requirement are included if a 
     * common alternative set of short month names is known to be used.
     * This means that users who can tell us of additional such encodings
     * may get them added to the list of supported languages by contacting
     * the jakarta-commons-net team.
     * </p>
     * <p><strong>
     * Please note that this attribute will NOT be used to determine a 
     * locale-based date format for the language.  </strong>  
     * Experience has shown that many if not most FTP servers outside the
     * United States employ the standard <code>en_US</code> date format 
     * orderings of <code>MMM d yyyy</code> and <code>MMM d HH:mm</code> 
     * and attempting to deduce this automatically here would cause more
     * problems than it would solve.  The date format must be changed 
     * via the {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr} and/or 
     * {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} parameters.
     * </p>
     * @param serverLanguageCode The value to set to the serverLanguageCode property.  
     */
    public void setServerLanguageCode(String serverLanguageCode) {
        this.serverLanguageCode = serverLanguageCode;
    }
    
    /**
     * Looks up the supplied language code in the internally maintained table of 
     * language codes.  Returns a DateFormatSymbols object configured with 
     * short month names corresponding to the code.  If there is no corresponding
     * entry in the table, the object returned will be that for 
     * <code>Locale.US</code> 
     * @param languageCode See {@link  #setServerLanguageCode(String)  serverLanguageCode}
     * @return a DateFormatSymbols object configured with short month names 
     * corresponding to the supplied code, or with month names for  
     * <code>Locale.US</code> if there is no corresponding entry in the internal
     * table.
     */
    public static DateFormatSymbols lookupDateFormatSymbols(String languageCode) 
    {
        Object lang = LANGUAGE_CODE_MAP.get(languageCode);
        if (lang != null) {
            if (lang instanceof Locale) {
                return new DateFormatSymbols((Locale) lang);
            } else if (lang instanceof String){
                return getDateFormatSymbols((String) lang);
            }
        }
        return new DateFormatSymbols(Locale.US);
    }
    
    /**
     * Returns a DateFormatSymbols object configured with short month names
     * as in the supplied string
     * @param shortmonths This  should be as described in 
     *  {@link  #setShortMonthNames(String)  shortMonthNames}
     * @return a DateFormatSymbols object configured with short month names
     * as in the supplied string
     */
    public static DateFormatSymbols getDateFormatSymbols(String shortmonths) 
    {
        String[] months = splitShortMonthString(shortmonths);
        DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
        dfs.setShortMonths(months);
        return dfs;
    }
    
    private static String[] splitShortMonthString(String shortmonths) {
        StringTokenizer st = new StringTokenizer(shortmonths, "|");
        int monthcnt = st.countTokens();
        if (12 != monthcnt) {
            throw new IllegalArgumentException(
                    "expecting a pipe-delimited string containing 12 tokens");
        }
        String[] months = new String[13];
        int pos = 0;
        while(st.hasMoreTokens()) {
            months[pos++] = st.nextToken();
        }
        months[pos]="";
        return months;
    }

    /**
     * Returns a Collection of all the language codes currently supported
     * by this class. See {@link  #setServerLanguageCode(String)  serverLanguageCode}  
     * for a functional descrption of language codes within this system. 
     *
     * @return a Collection of all the language codes currently supported
     * by this class
     */
    public static Collection<String> getSupportedLanguageCodes() {
        return LANGUAGE_CODE_MAP.keySet();
    }
    
    
}

 

 

    private static Map<String, Object> LANGUAGE_CODE_MAP = new TreeMap<String, Object>();

    static {

 

    这里的 LANGUAGE_CODE_MAP 里根本就没放zh,于是自己把它加进去:

 

        // many locales code short names for months as all three letters
        // these we handle simply.
        LANGUAGE_CODE_MAP.put("zh", Locale.CHINESE);
        LANGUAGE_CODE_MAP.put("en", Locale.ENGLISH);
        LANGUAGE_CODE_MAP.put("de",Locale.GERMAN);
 重新打包,部署,测试后上传后的在服务器端的文件依然是乱码,暂时无法解决。

 

 

 

分享到:
评论
2 楼 酒杯中的大海 2011-02-15  
学习了!!!~
1 楼 酒杯中的大海 2011-02-15  
牛逼,膜拜~

相关推荐

    Struts上传,已经处理好乱码问题

    首先,我们需要了解Struts文件上传的基本原理。在Struts中,文件上传是通过`Commons FileUpload`库来实现的,它是Apache Commons项目的一部分,专门处理HTTP请求中的多部分数据,包括文件上传。在Struts的ActionForm...

    无中文乱码的Struts上传下载(任意文件上传)

    本教程将详细介绍如何在Struts中实现无中文乱码的文件上传与下载,并涵盖任意数量的文件上传。 首先,解决中文乱码问题的关键在于配置正确的字符编码。在Struts的配置文件`struts-config.xml`中,你需要为...

    struts2文件上传(解决中文乱码)

    Struts2是一个强大的Java Web...总之,解决Struts2中的中文乱码问题,关键在于配置正确的字符编码,并在文件上传和下载过程中处理好编码转换。通过以上步骤,你可以有效地在Struts2项目中实现对中文URL和文件名的支持。

    Struts2文件上传下载 中文乱码

    在Struts2框架中,文件上传和下载是常见的功能需求,但处理中文文件名或内容时,可能会遇到中文乱码的问题。这个问题主要涉及到字符编码的处理,包括HTTP请求的编码、文件名的编码以及文件内容的编码。接下来,我们...

    Ext3.2 + struts 文件上传实例

    在本文中,我们将深入探讨如何使用Ext3.2与Struts框架实现文件上传功能,同时解决中文乱码问题。这两个技术结合在一起,可以为Web应用提供高效、稳定的文件上传解决方案。 首先,Ext3.2是一个JavaScript库,它提供...

    struts2文件下载(解决了中文乱码问题)

    在处理文件下载时,Struts2提供了一套完整的解决方案,包括处理中文文件名的乱码问题。在标题提到的场景中,开发者可能遇到了在使用Myeclipse 6.5集成开发环境,JDK1.6和Tomcat 6.0服务器环境下,通过Struts2进行...

    struts1的文件上传

    本文将深入探讨Struts1中的文件上传,包括单文件上传、多文件上传,以及解决文件名乱码和重名冲突等问题。 1. **文件上传基础** 文件上传是Web应用中常见的一种功能,允许用户从本地电脑上传文件到服务器。在...

    struts2中文乱码解决Demo

    7. **文件上传**:对于文件上传的情况,需要确保文件名和内容的编码也正确处理。Struts2提供了`Content-Type`和`filename*`头来处理非ASCII字符的文件名。 本DEMO应该包含了上述所有步骤的示例代码,你可以根据实际...

    struts2中的批量上传与文件下载(解决中文乱码问题)

    通过以上步骤,你可以实现在Struts2框架下的批量文件上传和下载,同时解决中文乱码问题。实践中,务必根据项目需求和安全规范进行适当调整。记得在你的项目中添加详细的错误处理和日志记录,以便于调试和排查问题。

    struts2上传下载 解决中文文件名乱码

    1. **配置Struts2拦截器**:Struts2的文件上传拦截器(`org.apache.struts2.interceptor.FileUploadInterceptor`)需要配置以支持UTF-8编码。在你的`struts.xml`配置文件中,可以添加以下配置: ```xml ...

    struts与hibernate实现文件的上传与动态下载

    通过以上步骤,你可以实现一个基于Struts2和Hibernate的文件上传与动态下载系统。这个系统能够处理用户上传的文件,将其保存到服务器,同时提供动态下载功能,允许用户根据需要下载文件。在实际开发中,还需要考虑...

    struts 2. 5.2解决中文乱码

    7. **文件上传**:在处理文件上传时,如Struts 2的`FileUpload`插件,需要确保文件名的编码正确处理,例如: ```java String fileName = new String(file.getName().getBytes("ISO-8859-1"), "UTF-8"); ``` 通过...

    struts实现上传无乱码

    本实例将详细介绍如何在Struts环境中使用Eclipse IDE和Tomcat服务器,通过添加过滤器解决文件上传过程中的乱码问题。 首先,我们需要在Struts配置文件(通常为struts.xml)中定义一个用于处理文件上传的Action。...

    struts文件带进度条上传,文件下载

    在本主题中,“struts文件带进度条上传,文件下载”指的是在Struts框架下实现带有进度条的文件上传与下载功能,同时解决中文乱码问题,并确保兼容各种浏览器。 **文件上传** 在Struts中,文件上传通常使用`struts2...

    02_张孝祥09年Struts视频_文件上传单元

    struts文件上传单元目录列表: 01.文件上传的网页设置与数据格式分析 02.阐释软件分层的设计思想 03.文件上传组件的实现原理与应用分析 04.用struts获取上传文件名和解决中文乱码 05.保存用struts获取的上传文件内容...

    11_张孝祥09年Struts视频_文件上传单元

    struts文件上传单元目录列表: 01.文件上传的网页设置与数据格式分析 02.阐释软件分层的设计思想 03.文件上传组件的实现原理与应用分析 04.用struts获取上传文件名和解决中文乱码 05.保存用struts获取的上传文件内容...

    struts中文件的上传

    在这个简单的Struts项目中,我们关注的重点是如何实现文件上传以及处理中文乱码问题。 首先,文件上传的实现涉及到前端HTML表单的设计和后端Action的处理。在HTML表单中,我们需要使用`&lt;input type="file"&gt;`元素来...

    使用struts2.0上传文件,完全解决了中文乱码问题

    使用struts2.0上传文件,完全解决了中文乱码问题。运行项目前请先在D盘建立个名tmp的文件夹,实例上传的文件将保存在tmp文件夹中。文件大小限制为15M,该示例蛮完整的,有些架包忘删了导致项目有点大。

    struts文件的上传下载功能实现

    以上就是Struts框架下文件上传和下载的基本实现,以及如何处理中文文件名乱码的问题。实际开发中,还需要考虑文件大小限制、安全性(如防止路径遍历攻击)等问题,以确保系统的稳定性和安全性。

    使用struts上传文件

    在本文中,我们将深入探讨如何在Struts中实现文件上传,并结合`SetCharacterEncodingFilter.java`这个文件名,讨论字符编码的重要性。 首先,文件上传在Web应用中是一个常见的需求,例如用户可能需要上传头像、简历...

Global site tag (gtag.js) - Google Analytics