Smartupload Chinese File Name BUG Correction

xiaoxiao2021-03-06  75

Since the smartupload Chinese download cannot be supported, after the anti-compilation, the discovery file is read, and the Chinese file can be attached to the browser head, the error, no prompt download, may be sent by UTF-8 due to most IE browser URL, so convert the Chinese file name to UTF-8 to solve the problem. Smartupload.java ============================================== ============================================== // Decompiled by jad v1.5.7g. Copyright 2000 Pavel KouznetSov.// jad Home Page: http://www.geocities.com/siliconvalley/bridge/8617/jad.html// Decompiler Options: Packimports (3) Fieldsfirst Ansi // Source File Name: SmartUpload.java

Package com.jspsmart.upload;

import java.io *;. import java.sql.ResultSet; import java.sql.SQLException; import java.util.Vector; import javax.servlet *;. import javax.servlet.http.HttpServletRequest; import javax.servlet.http .HttpservletResponse;

// Reference Classes of Package Com.jspsmart.upload: // Files, Request, File, SmartUploadexception

Public class smartupload {

protected byte m_binArray []; protected HttpServletRequest m_request; protected HttpServletResponse m_response; private int m_totalBytes; private int m_currentIndex; private int m_startData; private int m_endData; private String m_boundary; private long m_totalMaxFileSize; private long m_maxFileSize; private Vector m_deniedFilesList; private Vector m_allowedFilesList; private boolean m_denyPhysicalPath; private boolean m_forcePhysicalPath; private String m_contentDisposition; public static final int SAVE_AUTO = 0; public static final int SAVE_VIRTUAL = 1; public static final int SAVE_PHYSICAL = 2; private Files m_files; private Request m_formRequest; private ServletConfig config; public final Void Init (servletconfig config) throws servletexception {this.config = config;

Protected final servletconfig getServletconfig () {return config;

public SmartUpload () {m_totalBytes = 0; m_currentIndex = 0; m_startData = 0; m_endData = 0; m_boundary = new String (); m_totalMaxFileSize = 0L; m_maxFileSize = 0L; m_deniedFilesList = new Vector (); m_allowedFilesList = new Vector (); M_ForcePhysicalPath = false; m_contentdisposition = new string (); m_files = new files (); m_FormRequest = new request ();

Public void service (httpservletRequest request, httpservletResponse response) throws ioException, servletexception {m_request = request; m_response = response;}

public void upload () throws IOException, ServletException {int totalRead = 0; int readBytes = 0; long totalFileSize = 0L; boolean found = false; String dataHeader = new String (); String fieldName = new String (); String fileName = new String (); String fileExt = new String (); String filePathName = new String (); String contentType = new String (); String contentDisp = new String (); String typeMIME = new String (); String subTypeMIME = new String ( ); boolean isFile = false; m_totalBytes = m_request.getContentLength (); m_binArray = new byte [m_totalBytes]; for (; totalRead

if (m_currentIndex == 1) return; m_currentIndex ; do {if (m_currentIndex> = m_totalBytes) break; dataHeader = getDataHeader (); m_currentIndex = m_currentIndex 2; isFile = dataHeader.indexOf ( "filename")> 0; fieldName = getDataFieldValue (dataHeader, "name"); if (isFile) {filePathName = getDataFieldValue (dataHeader, "filename"); fileName = getFileName (filePathName); fileExt = getFileExt (fileName); contentType = getContentType (dataHeader); contentDisp = getContentDisp (dataHeader ); typeMIME = getTypeMIME (contentType); subTypeMIME = getSubTypeMIME (contentType);} getDataSection (); if (isFile && fileName.length ()> 0) {if (m_deniedFilesList.contains (fileExt)) throw new SecurityException ( "T he extension of the file is denied to be uploaded (1015). ");! if (! m_allowedFilesList.isEmpty () && m_allowedFilesList.contains (fileExt)) throw new SecurityException (" The extension of the file is not allowed to be uploaded . (1010) "); if (m_maxFileSize> (long) 0 && (long) ((m_endData - m_startData) 1)> m_maxFileSize) throw new SecurityException (String.valueOf ((new StringBuffer (" Size exceeded for this file: ")). Append (filename) .append (" (1105). "))); TotalFileSize =

(M_endData - m_startData) 1; if (m_totalMaxFileSize> (long) 0 && totalFileSize> m_totalMaxFileSize) throw new SecurityException ( ". Total File Size exceeded (1110)");} if (isFile) {com.jspsmart.upload.File newFile = new com.jspsmart.upload.File (); newFile.setParent (this); newFile.setFieldName (fieldName); newFile.setFileName (fileName); newFile.setFileExt (fileExt); newFile.setFilePathName (filePathName); newFile. setIsMissing (filePathName.length () == 0); newFile.setContentType (contentType); newFile.setContentDisp (contentDisp); newFile.setTypeMIME (typeMIME); newFile.setSubTypeMIME (subTypeMIME); if (contentType.indexOf ( "application / x -macbinary ")> 0) m_startdata = m_startdata 128; newfile.setsize ((m_enddata - m_st artData) 1); newFile.setStartData (m_startData); newFile.setEndData (m_endData); m_files.addFile (newFile);} else {String value = new String (m_binArray, m_startData, (m_endData - m_startData) 1); m_formRequest .putParameter (FieldNameter (FieldNameter);} IF ((char) m_binarray [m_currentIndex 1] == '-') Break; M_CurrentIndex = M_CurrentIndIndex 2;} while (true);}

public int save (String destPathName) throws SmartUploadException, IOException, ServletException {return save (destPathName, 0);} public int save (String destPathName, int option) throws SmartUploadException, IOException, ServletException {int count = 0; if (destPathName == NULL) DESTPATHNAME = config.getServletContext (). getRealPath ("/"); if (destpathname.indexof ("/")! = -1) {if (destPathname.Charat (destPathname.length () - 1)! = ' / ') DestPathname = String.Valueof (destpathname) .concat ("/");} else if (destPathname.Charat (DESTPATHNAME.LENGTH () - 1)! =' // ') destPathname = String.Valueof (destpathname) .concat ("//"); for (int i = 0; i

Return count;

Public int getSize () {return m_totalbytes;}

public byte getBinaryData (int index) {byte retval; try {retval = m_binArray [index];} catch (Exception e) {throw new ArrayIndexOutOfBoundsException ( "Index out of range (1005).");} return retval;}

Public files getfiles () {return m_files;}

Public Request getRequest () {return m_formRequest;}

public void downloadFile (String sourceFilePathName) throws IOException, ServletException {downloadFile (sourceFilePathName, null, null);} public void downloadFile (String sourceFilePathName, String contentType) throws IOException, ServletException {downloadFile (sourceFilePathName, contentType, null);}

public void downloadFile (String sourceFilePathName, String contentType, String destFileName) throws IOException, ServletException {downloadFile (sourceFilePathName, contentType, destFileName, 65000);}

public void downloadFile (String sourceFilePathName, String contentType, String destFileName, int blockSize) throws IOException, ServletException {if (sourceFilePathName == null) throw new IllegalArgumentException (String.valueOf ((new StringBuffer ( "File '")). append (sourceFilePathName ) .append ("'NOT FOUND (1040))))))); if (SourceFilepathname.equals (")) Throw new IllegalargumentException (String.Valueof ("File')). Append (SourceFilePathname) .append ( " 'not found (1040)."))); if (isVirtual (sourceFilePathName) && m_denyPhysicalPath) throw new SecurityException ()! "Physical path is denied (1035)."; if (isVirtual (sourceFilePathName)) sourceFilePathName . = config.getServletContext () getRealPath (sourceFilePathName); java.io.File file = new java.io.File (sourceFilePathName); FileInputStream fileIn = new FileInputStream (file); long fileLen = file.length (); int readBytes = 0; INT TOTALREAD = 0; byte b [] = new byte [blocksize]; if (contentty == null) m_response.setContentType ("Application / X-msdownload"); Else IF (ContentType.length () == 0) m_Response. setContentType ( "application / x-msdownload"); else m_response.setContentType (contentType); m_response.setContentLength ((int) fileLen); if (destFileName == null) {m_response.addHeader ( "Content-Disposition", "attachment; Filename = " Toutf8String (getFileName (SourceFilePathname));

} Else if (destFileName.length () == 0) m_response.addHeader ( "Content-Disposition", "attachment;"); else m_response.addHeader ( "Content-Disposition", "attachment; filename =". Concat (toUtf8String (DESTFILENAME))); While ((long) {readBytes = filein.read (b, 0, blocksize); TotalRead = readBytes; m_response.getoutputstream (). Write (B, 0, readbytes);} Filein.Close ();

public void downloadField (ResultSet rs, String columnName, String contentType, String destFileName) throws SQLException, IOException, ServletException {if (rs == null) throw new IllegalArgumentException ( "The RecordSet can not be null (1045)."); if (columnName == null) throw new IllegalArgumentException ( "The columnName can not be null (1050)."); if (columnName.length () == 0) throw new IllegalArgumentException ( "The columnName can not be empty (1055)."); byte b [] = rs.getBytes (columnName); if (contentType == null) m_response.setContentType ( "application / x-msdownload"); else if (contentType.length () == 0) m_response.setContentType ( "application / x-msdownload "); else m_response.setContentType (contentType); m_response.setContentLength (b.length); if (destFileName == null) m_response.addHeader (" Content-Disposition "," attachment; "); else if (destFileName.length () == 0) m_response.addHeader ( "Content-Disposition", "attachment;"); else m_response.addHeader ( "Content-Disposition", "attachment; filename =". concat (toUtf8String (destFileName )))); M_response.getOutputStream (). Write (b, 0, b.length);}

/ ** * turn the Chinese characters in the file name into the UTF8 encoded string so that you can correctly display the saved file name. * @Param s Original file name * @return Recode file name * / public static string toutf8string String s) {stringbuffer sb = new stringbuffer (); for (int i = 0; i = 0 && c <= 255) {sb.append (c);} else {byte [] b; try {b = character.tostring (c) .getbytes ("UTF-8");} catch (exception ex) {system.out.println (EX); b = new byte [0];} for (int J = 0; j

Private string getDatafieldValue (string toolname) {string token = new string (); string value = new string (); int pos = 0; int = 0; int start = 0; int end = 0; token = string .valueof (new stringbuff)). Append ("="). append ('")); POS = DataHeader.indexof (token); if (POS> 0) {i = POS token.Length (); start = i; token = "/" "; end = DataHeader.indexof (token, i); if (start> 0 && end> ​​0) Value = DataHeader.substring (start, end); } Return Value;} private string getfileext (string filename) {string value = new string (); int start = 0; int end = 0; if (filename == null) Return Null; start = filename.lastIndexof (46) 1; end = filename.length (); value = filename.substring (start, end); if (filename.lastIndexof (46)> 0) Return Value; Else Return "";

private String getContentType (String dataHeader) {String token = new String (); String value = new String (); int start = 0; int end = 0; token = "Content-Type:"; start = dataHeader.indexOf (token ) token.length (); if (start! = -1) {end = DataHeader.Length (); value = dataHeader.substring (start, end);} return value;}

Private string gettypemime (string limited) {string value = new string (); int pos = 0; pOS = contentty.indexof ("/"); if (pOS! = -1) Return ContentType.Substring (1, POS); Else Return ContentType;} Private String getSubtypeMime (String value = new string (); int start = 0; int end = 0; start = contenttype.indexof ("/") 1; if (start! = - 1) {end = contentType.Length (); return contenttype.substring (start, end);} else {return contenttype;}}

Private string getContentDisp (string value = new string (); int start = 0; int end = 0; start = dataHeader.indexof (":") 1; end = dataHeader.indexof (";"); Value = DataHeader.substring (start, end); return value;}

private void getDataSection () {boolean found = false; String dataHeader = new String (); int searchPos = m_currentIndex; int keyPos = 0; int boundaryLen = m_boundary.length (); m_startData = m_currentIndex; m_endData = 0; do {if ( SearchPos> = m_totalbytes) Break; if (m_binarray [searchpos] == (byte) m_boundary.charat (keypos)) {if (keypos == Boundarylen - 1) {m_enddata = ((SearchPos - Boundarylen) 1) - 3; break;} searchPos ; keyPos ;} else {searchPos ; keyPos = 0;}} while (true); m_currentIndex = m_endData boundaryLen 3;} private String getDataHeader () {int start = m_currentIndex; int end = 0; int len = 0; Boolean Found = false; while (! F) ound) if (m_binArray [m_currentIndex] == 13 && m_binArray [m_currentIndex 2] == 13) {found = true; end = m_currentIndex - 1; m_currentIndex = m_currentIndex 2;} else {m_currentIndex ;} String dataHeader = new String (M_Binarray, Start, (end - start) 1); return dataheader;}

Private string getFileName (string filepathname) {string token = new string (); string value = new string (); int pos = 0; int = 0; int start = 0; int end = 0; pOS = filepathname.lastIndexof 47); if (POS! = -1) return filepathname.substring (POS 1, FilePathname.Length ()); POS = filepathname.lastIndexof (92); if (POS! = -1) Return FilePathname.substring (POS 1, filePathName.length ()); else return filePathName;} public void setDeniedFilesList (String deniedFilesList) throws SQLException, IOException, ServletException {String ext = ""; if (deniedFilesList = null) {ext =! ""; for ( INT i = 0; i

IF (ext! = ") m_deniedfileslist.addelement (ext);} else {m_deniedfileslist = null;}}

public void setAllowedFilesList (String allowedFilesList) {String ext = ""; if (! allowedFilesList = null) {ext = ""; for (int i = 0; i

Public void setdenyphysicalpath (boolean deny) {m_denyphysicalpath = deny;}

Public void setforcephysicalpath (boolean force) {m_forcephysicalPath = force;

Public void setContentDisPosition (String contentdisposition) {m_contentdisposition = contentdisposition;

Public void settotalmaxfilesize (long TotalmaxFilesize) {m_totalmaxfilesize = TotalmaxFileSize;}

Public void setmaxfilesize (long maxfilesize) {m_maxfilesize = maxfilesize;}

protected String getPhysicalPath (String filePathName, int option) throws IOException {String path = new String (); String fileName = new String (); String fileSeparator = new String (); boolean isPhysical = false; fileSeparator = System.getProperty ( "file .separator "); if (filePathName == null) throw new IllegalArgumentException (" There is no specified destination file (1140) "); if (filePathName.equals (". ")) throw new IllegalArgumentException (" There is no specified destination File (1140). "); if (" // ")> = 0) {path = filepathname.substring (0, filepathname.lastIndexof (" // ")); filename = filepathname.substring (filepathname .lastindexof ("//") 1);} if (filepathname.lastindexof ("/")> = 0) {path = filepathname.substring (0, filepathname.lastIndexof ("/")); filename = filepathname. Substring (filepathname.lastindexof ("/" ) 1);} path = path.length ()! = 0? Path: "/"; java.io.file physicalPath = new java.io.file (path); if (PhysicalPath.exists ()) isphysical = True; if (option == 0) {if (isvirtual (path)) {path = config.getServletContext (). getRealPath (path); if (path.endswith (fileseparator)) Path = Path FileName; Else Path =

String.valueOf ((new StringBuffer (String.valueOf (path))) append (fileSeparator) .append (fileName).); Return path;} if (isPhysical) {if (m_denyPhysicalPath) throw new IllegalArgumentException ( "Physical path is denied (1125). "); Else Return Filepathname;} else {throw new IllegalargumentException (" this path does not exist (1135). ");}} F (option == 1) {IF (isvirtual (path) {pat . = config.getServletContext () getRealPath (path); if (path.endsWith (fileSeparator)) path = path fileName;. else path = String.valueOf ((new StringBuffer (String.valueOf (path))) append (fileSeparator ) .append (filename)); return path;} IF (isphysical) Throw new iLlegaLaMumeTexception "The path is not a virtual path."); Else throw new IllegalArgumentException ( "This path does not exist (1135).");} If (option == 2) {if (isPhysical) if (m_denyPhysicalPath) throw new IllegalArgumentException ( "Physical path is denied (1125)."); else return filePathName; if (isVirtual (path)) throw new IllegalArgumentException ( "The path is not a physical path."); else throw new IllegalArgumentException ( "This path does not Exist (1135). ");} else {return null;

}} Public void uploadInFile (String destFilePathName) throws SmartUploadException, IOException {int intsize = 0; int pos = 0; int readBytes = 0; if (destFilePathName == null) throw new IllegalArgumentException ( "There is no specified destination file (1025) . "); if (destFilePathName.length () == 0) throw new IllegalArgumentException (" There is no specified destination file (1025) "); if (! isVirtual (destFilePathName) && m_denyPhysicalPath) throw new SecurityException (". Physical path is denied (1035) ");. intsize = m_request.getContentLength (); m_binArray = new byte [intsize]; for (; pos

if (isVirtual (destFilePathName)) destFilePathName = config.getServletContext () getRealPath (destFilePathName);. try {java.io.File file = new java.io.File (destFilePathName); file.createNewFile (); FileOutputStream fileOut = new FileOutputStream (file); FileOut.write; fileout.close ();} catch (exception e) {throw new smartuploadext ("The Form Cannot Be Saved In The Specified File (1030));}}

转载请注明原文地址:https://www.9cbs.com/read-93215.html

New Post(0)