- Upload files
. A jar introducing two
apache: commons-fileupload.jar
components
commons-fileupload.jar
dependcommons-io.jar
b.
Code:
Front jsp:
<input type="file" name="spicture"/>
Form submission must be post
in 表单
must add a property in entype="multipart/form-data"
the background servlet:
- Attention to the problem:
Upload directory upload:-
If you modify the code, at the time of restart tomcat will be deleted
Cause: When modifying code, tomcat will recompile and redeploy a class (re-create various catalogs) -
If you do not modify the code, it does not remove the
reason: there is no code changes, class is still before the class
-
Therefore, in order to prevent the upload directory is missing:.. A virtual path b direct replacement of non-tomcat upload directory to directory
Limit upload:
type,
size
attention to restrictions on the file before writing again parseRequest
- Download: dependency on any JAR
A request (address a form), the request Servlet.
B.Servlet address file through the file input stream read into the Servlet
c through an output stream has just been input stream into an output file. user
Note: download the file header in response to set two:
Response.AddHeader ( "the type-Content", "file application / OCTET-Stream"); // the MIME types: binary file (an arbitrary file)
Response.AddHeader ( "Content- Disposition "," attachement; filename = "+ fileName); // fileName contains the file suffix: abc.txt