js| Upload | question
It turns out that the world is a circle. All the while I was moving in circles, from the beginning to the beginning, from the end to the end. Searched countless data on the Internet, in order to find the text form and file files submitted together with the method, tired head straight ring. Finally, back to where I started. I found the code that I had ignored. It turns out that it can be so simple.
OK, I cut to the chase, the following examples from the Internet, which predecessors wrote the content, I just a little change, write this article no commercial purposes, seniors forgive me did not introduce your name AH.
The program has a submission page, in fact, with HTML, but the original code with JSP I also take it.
selectfile.jsp---->web.xml >servletupload.java is basically the same structure.
Here's the code:
selectfile.jsp
<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<title>file upload</title>
<body>
<font size= "5" color= "#FF0000" >
<b> File upload-use Jspsmart upload components </b>
</font><br>
<form name= "Selectfile" enctype= "Multipart/form-data" method= "post" action= "Servletupload" >
<p> file Name:
<input type= "File" Name= "Ulfile" size= "maxlength=" ><br>
</p>
<p> Upload path:
<input type= "text" name= "PATH" size= "maxlength=" ><br>
</p>
<p> Additional content:
<input type= "text" Name= "other" size= "maxlength=" ><br>
</p>
<p>
<input type= "Submit" value= "Upload" >
<input type= "Reset" value= "clear" >
</p>
</form>
</body>
//servletupload.java
Import java.io.*;
Import java.util.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import com.jspsmart.upload.*;
Public class Servletupload extends HttpServlet {
private servletconfig config;
/**
* initialization servlet
*/
final public void init (ServletConfig config) throws servletexception {
this.config = config;
&NBSP}
/**
* processing Get requests
*/
public void doget (httpservletrequest Request, HttpServletResponse response) throws Servletexception, IOException {
printwriter out = Response.getwriter ();
out.println ("<HTML>");
out.println ("<body bgcolor= ' White ' >");
out.println (" out.println ("<HR><BR>");
out.println ("The method of the HTML form must is POST.");
out.println ("</BODY>");
out.println ("</HTML>");
}
/**
* response POST request
*/
protected void DoPost (HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {
printwriter out = Response.getwriter ();
out.println ("<HTML>");
out.println ("<body bgcolor= ' White ' >");
out.println (" out.println ("<HR>");
Variable definition
int count=0;
Smartupload mysmartupload = new Smartupload ();
try {
Class
Mysmartupload.initialize (Config,request,response);
Upload
Mysmartupload.upload ();
Com.jspsmart.upload.File f1 = Mysmartupload.getfiles (). GetFile (0);
String name = F1.getfilename ();
SYSTEM.OUT.PRINTLN (name);
Save upload file to specified directory
Path to the form form submission.
Count = Mysmartupload.save (Mysmartupload.getrequest (). GetParameter ("PATH"));
The other is submitted for form form.
String other=mysmartupload.getrequest (). GetParameter ("other"); This can be done with the other
Request.getparameter ("PATH"); Request.gerparameter ("other");
Show processing results
OUT.PRINTLN (Count + "file uploaded.");
catch (Exception e) {
Out.println ("Unable to upload the file.<br>");
Out.println ("Error:" + e.tostring ());
}
Out.println ("</BODY>");
Out.println ("</HTML>");
}
}
The Web.xml configuration is as follows:
<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "http://java.sun.com/dtd/web-app_2_ 3.dtd">
<web-app>
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>servletUpload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>servletUpload</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>selectfile.jsp</welcome-file>
</welcome-file-list>
</web-app>
Need to introduce in the Web-inf\lib Jspsmart this bag, the internet to look for, there are many under www.jspsmart.com here is his official website. Put the compiled class file in the web-inf\ Classes can be run under the.
The Mysmartupload.getrequest () getparameter ("other") provided by Jspsmart is used in this area. This thing, because at the beginning feel that the path address is not necessary to pass the code out early, and then want to use Request.getparameter ("") this to get information, but always error. Found more than n articles on the Internet, Many people face the same difficulties. So they want to use a logical relationship to avoid this situation. is to use a separate form to upload with another form to the database input. But when you do not get the file name to upload, I'm trying to save the file name in the database. If you want to have to put it into the session, I think this is too troublesome, not easy to make a bug, if the temporary information into the database, there are many people to operate together is a problem, It also encounters the problem of writing information to the property value of file. Can only read, can not write, that is the result. Every time it's a quick success, it's stuck in a small place like this. So the Internet to find other components to see if there is a corresponding function. This time using the FileUpload component, Users use the situation to see this is also better than Jspsmart but also did not find getparameter such a method.
So continue to search on the Internet, the results found their original use of the code, found that ... The original Mysmartupload.getrequest (). The getparameter can be realized. It's a big sweat. Now it's going to work. But maybe later it will be changed to other components, so that the uploaded data more stable. This is the first time Business logic has come true.