JavaWeb - upload, download

  1. Upload files
    . A jar introducing two
    apache: commons-fileupload.jarcomponents
    commons-fileupload.jardepend commons-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:
    1. 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)

    2. 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

  1. 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
Published 41 original articles · won praise 1 · views 556

Guess you like

Origin blog.csdn.net/qq_41620020/article/details/104946063