Struts file upload component org.apache.struts.upload
Using multiple Java file upload components, it is not too convenient, or the compatibility of Windows and Linux is not very good, or can only process file uploads, and cannot handle files and other form information. Try with Struts UPLOAD components before two days, modify an Action, ActionForm, and FTL templates in my project, making them support org.apache.struts.upload. It is very convenient to start, and it is successful, but open the files after the upload, but encounters the prompt of the file error. The DOC file cannot be opened. The picture inside JPG can only see half, and the txt file is open, but it also prompts the character error. No matter whether the file size is 10K or 5M is the same. Once again, I will find the reason. There are several articles to explain the reason, saying that the Struts source code is required to modify the Struts source code to recompile the JAR package. Day, too much trouble, is there a simple way? I use Struts1.1, I used its source code, but I don't know where to put it. I don't know if the Apache website has 1.1 source code. Fortunately, I remember the source code of Struts1.1 in JBuilderx, in C: /JBuilderx/extras/jakarta-struts-1.1-src/src. Open the file in C: /JBuilderx/extras/jakarta-struts-1.1-src/src/share/org/apache/struts/upload, see Doc-Files There is a UPLOADUML.JPG under Doc-Files, which clearly indicates each file. Relationship. But it is actually useless. Later, look carefully, find an example of c: /jbuilderx/extras/jakarta-struts-1.1-src/src/upload/org/apache/struts/webapp/upload. Carefully compare your own Action, finally found some different, originally read with InputStream, OutputStream writes. After converting to my project actual code as follows: formfile file = form.getupfile (); string realpath = this.getServlet (). GetServletContext (). GetRealPath ("/ uploads /"); try {inputstream stream = file.getinputStream ); OutputStream bos = new fileoutputstream (RealPath "//" file.getFileName ()); int BytesRead = 0; Byte [] buffer = new byte [8192]; while ((BytesRead = stream.read (buffer, 0, 8192))! = -1) {bos.write (buffer, 0, bytesread);} bos.close ();
} catch (ioexception ie) {// handle exceptions} recompiled my Action, upload test, all of the problems all resolved. OK. Development environment: JBX, JDK1.5, Tomcat4.1.27, Struts1.1. XIAOZHEN.com Attached: After the file was written, he suddenly noted that the buffer settings here were 8192, and I used to use another way to meet the files, when I can upload half, this value seems to be 1024, I don't know if it is with it. related. Try again, interested friends can try themselves.