您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 其它办公文档 > 解决jspSmartUpload组件上传下载文件时中文乱码问
真正解决jspSmartUpload组件上传下载文件时中文乱码问题!!!2008-10-2816:322294人阅读评论(23)收藏举报1:当页面上传有参数时,中文会乱码,解决页面上中文参数乱码方法:修改类SmartUpload()中的upload()方法publicvoidupload()throwsServletException,IOException,SmartUploadException{inti=0;//booleanflag=false;booleanflag1=false;//booleanflag2=false;longl=0L;//Strings=;//newString();//Strings2=;//newString();Strings4=;//newString();Strings5=;//newString();Strings6=;//newString();Strings7=;//newString();Strings8=;//newString();Strings9=;//newString();Strings10=;//newString();m_totalBytes=m_request.getContentLength();m_binArray=newbyte[m_totalBytes];intj;for(;im_totalBytes;i+=j){try{m_request.getInputStream();j=m_request.getInputStream().read(m_binArray,i,m_totalBytes-i);}catch(Exceptionexception){thrownewSmartUploadException(Unabletoupload.);}}for(;!flag1&&m_currentIndexm_totalBytes;m_currentIndex++){if(m_binArray[m_currentIndex]==13){flag1=true;}else{m_boundary=m_boundary+(char)m_binArray[m_currentIndex];}}if(m_currentIndex==1){return;}for(m_currentIndex++;m_currentIndexm_totalBytes;m_currentIndex=m_currentIndex+2){Strings1=getDataHeader();m_currentIndex=m_currentIndex+2;booleanflag3=s1.indexOf(filename)0;Strings3=getDataFieldValue(s1,name);if(flag3){s6=getDataFieldValue(s1,filename);s4=getFileName(s6);s5=getFileExt(s4);s7=getContentType(s1);s8=getContentDisp(s1);s9=getTypeMIME(s7);s10=getSubTypeMIME(s7);}getDataSection();if(flag3&&s4.length()0){if(m_deniedFilesList.contains(s5)){thrownewSecurityException(Theextensionofthefileisdeniedtobeuploaded(1015).);}if(!m_allowedFilesList.isEmpty()&&!m_allowedFilesList.contains(s5)){thrownewSecurityException(Theextensionofthefileisnotallowedtobeuploaded(1010).);}if(m_maxFileSize0L&&(long)((m_endData-m_startData)+1)m_maxFileSize){thrownewSecurityException(Sizeexceededforthisfile:+s4+(1105).);}l+=(m_endData-m_startData)+1;if(m_totalMaxFileSize0L&&lm_totalMaxFileSize){thrownewSecurityException(TotalFileSizeexceeded(1110).);}}if(flag3){SmartFilefile=newSmartFile();file.setParent(this);file.setFieldName(s3);file.setFileName(s4);file.setFileExt(s5);file.setFilePathName(s6);file.setIsMissing(s6.length()==0);file.setContentType(s7);file.setContentDisp(s8);file.setTypeMIME(s9);file.setSubTypeMIME(s10);if(s7.indexOf(application/x-macbinary)0){m_startData=m_startData+128;}file.setSize((m_endData-m_startData)+1);file.setStartData(m_startData);file.setEndData(m_endData);m_files.addFile(file);}else{/***原来的代码,当页面上要传入中文参数时,乱码。*///Strings11=newString(m_binArray,m_startData,(m_endData-m_startData)+1);/***2008-10-28carton修改,解决页面传入中文参数乱码*/Strings11=newString(m_binArray,m_startData,(m_endData-m_startData)+1,utf-8);m_formRequest.putParameter(s3,s11);}if((char)m_binArray[m_currentIndex+1]=='-'){break;}}}2:当上传时文件名为中文时,解决中文乱码修改类SmartUpload()中的getDataHeader()方法privateStringgetDataHeader(){//booleanflag=false;inti=m_currentIndex;intj=0;for(booleanflag1=false;!flag1;){if(m_binArray[m_currentIndex]==13&&m_binArray[m_currentIndex+2]==13){flag1=true;j=m_currentIndex-1;m_currentIndex=m_currentIndex+2;}else{m_currentIndex++;}}/***原代码,当上传文件名为中文时,乱码*///Strings=newString(m_binArray,i,(j-i)+1);//returns;/***2008-10-28carton修改,解决上传文件中文乱码问题*/Stirngs=null;try{s=newString(m_binArray,i,(j-i)+1,utf-8);}catch(UnsupportedEncodingExceptione){e.printStackTrace();}returns;}这样页面上就不用再转码了。页面设置成utf-8编码即可。附改好的jar包,和jspSmartUpload源代码.
本文标题:解决jspSmartUpload组件上传下载文件时中文乱码问
链接地址:https://www.777doc.com/doc-4624635 .html