JSP file upload function to solve the doc, ppt, xls file contents garbage problem

Category: Web Develop
 
jaysonchen
2010-08-05 06:28:58

Sponsored Links
Only txt format text documents is possible, other formats are either open the inside is garbled, or direct file error, open, seek large cattle guidance.

submission page form form:
<form action = "upload.jsp" method = "post" enctype = "multipart / form-data" >
<table border="1" width="1000" height="500" id="sidebar">
<% - - Type enctype using multipart / form-data, so you can put data in the file as a stream of data upload, no matter what file types can be uploaded. -%>
<tr> <td> <font Color="red"> Please select a file to upload: </font> <input type = "file" ; name = "upfile" size = "50" /> </td>

<td> <input type = "submit" ; value = "Submit"> </td> </tr>
</table> </form>

processing page code:
<% @ page contentType = "text / html; charset = gbk"%>
<% @ page import = "java.io. *" %>
<% @ page import = "java.util. *"%>
<% @ page import = "javax.servlet. * "%>
<% @ page import =" javax.servlet.http. * "%>
<% request.setCharacterEncoding (" gbk "); %>
<html>
<body bgcolor="#ffffff">
<% String showtitle = request.getParameter ("loadfile" );
if (showtitle! = null) {
showtitle = new String (request.getParameter ("loadfile"). getBytes ("ISO-8859-1"), "gbk" );
}
%>
<% = showtitle%>
<%
/ / define the maximum number of bytes uploaded files
int MAX_SIZE = 102400 * 102400;
/ / Create the root path to save the variable
String rootPath;
/ / read into the class declaration file
DataInputStream in = null;
FileOutputStream fileOut = null;
/ / get the client's network address
String remoteAddr = request. getRemoteAddr ();

/ / get the server name
String serverName = request.getServerName ();

/ / get the absolute Internet applications Address
String realPath = request.getRealPath (serverName);

realPath = realPath.substring (0, realPath.lastIndexOf ("\ \")) ;

/ / Create a folder to save
rootPath = realPath + "\ \ upload \ \";

/ / Get the client to upload the data type
String contentType = request.getContentType ();
try {
if (contentType.indexOf ("multipart / form-data") ;> = 0) {
/ / read the data uploaded
in = new DataInputStream (request.getInputStream ());
int formDataLength = ; request.getContentLength ();
if (formDataLength> MAX_SIZE) {
out.println ("<P> uploaded files can not exceed the number of bytes" + MAX_SIZE + "</p>");
return;
}
/ / save the uploaded file data
byte dataBytes [] ; = new byte [formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
/ / upload data stored in the byte array
while (totalBytesRead <formDataLength) {
byteRead = in.read (dataBytes, totalBytesRead, formDataLength);
totalBytesRead + = byteRead;
}
/ / create a string according to a byte array
String file = new String (dataBytes);
/ / out.println (file);

String saveFile = "3624354.xls" ;/ / I personally want to save the file name into the system time plus the original file extension, if you can, the way to help solve it. I was directly specify the format of the saved names and suffixes
int lastIndex = contentType.lastIndexOf ("=");
/ / get the data delimited string
String ; boundary = contentType.substring (lastIndex + 1, contentType.length ());
/ / create a path to save the file name
String fileName = rootPath + saveFile;
/ / out.print (fileName);
int pos;
pos = file.indexOf ("filename =");
pos = file.indexOf ("n", pos) + 1;
pos = file.indexOf ("n", pos) + 1;
pos = file.indexOf ("n", pos) + 1;
int boundaryLocation = file.indexOf (boundary, pos) - ; 4;
/ / out.println (boundaryLocation);
/ / get file data starting position
int startPos = ((file.substring (0, pos) ). getBytes ()). length;
/ / out.println (startPos);
/ / get the position of the end of the file data
int endPos = ((file. substring (0, boundaryLocation)). getBytes ()). length;
/ / out.println (endPos);
/ / check upload file exists
File checkFile = new File (fileName);
if (checkFile.exists ()) {
out.println ("<p>" + saveFile + "file already exist. </p> ");
}
/ / check upload files directory exists
File fileDir = new File (rootPath);
if (! fileDir.exists ()) {
fileDir.mkdirs ();
}
/ / create a file to write class
fileOut = new FileOutputStream (fileName);
/ / save the file data
fileOut.write (dataBytes, startPos, (endPos - startPos));
fileOut.close ();
out.println (saveFile + "file uploaded successfully. </p>");
} else {
String content = request.getContentType ();
out.println ("<p> uploaded data type is not multipart / form-data </ ​​p>");
}
} catch (Exception ex) {
throw new ServletException (ex.getMessage ());
}
%>
</body>
</html>


Sponsored Links

COOR707
2010-08-05 06:31:43
binary upload it
I generally do not garbled space code has smartupload upload hope useful to you
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.