Ideaj+maven+javaweb Practice: Sevlet Implement Upload&download,javaio code

Source: Internet
Author: User

Because the working machine does not let the copy out also does not allow the mail to come out also does not allow the access outside the network, therefore the file two PC moves to move is very troublesome.

decided to write a webpage that only upload and download

Ideaj,maven,java,tomcat wrote a javaweb.

The construction process is as follows:

http://blog.csdn.net/myarrow/article/details/50824793

Ideaj+maven+javaweb

1. Paid version Ideaj

2.file-new-project-maven-maven_archetype_webapp-Set GroupID and artifectid-save

3.ctrl+shift+alt+s Configuration

4. Configure Tomcat

New tomcat-server-local

5.mvn-clean,mvn-compile-start server-localhost:8080

Write the code as follows:

1. Create a class, the servlet

2. Create a JSP page to invoke the servlet

3. Add a mapping relationship to the servlet in Web. xml

A simple dopost and Doget servlet is as follows:

1. Dogetdemo.java

Package com.test;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Dogetdemo extends HttpServlet {
public void doget (httpservletrequest request,httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("text/html;charset=gb2312");
PrintWriter out = Response.getwriter ();
Request.setcharacterencoding ("gb2312");
String username = request.getparameter ("username");
String Password = request.getparameter ("password");
Out.println ("Out.println ("<body>");
Out.println ("User name:" + username + "<br>");
Out.println ("Password:" + password);
Out.println ("</body>");
Out.println ("}
}

2. userform_dogetdemo.jsp
<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
<title> User Forms </title>
<body>
<form action= "Dogetdemo" method= "Get" >
User name: <input type= "text" name= "username"/><br>
Password: <input type= "password" name= "password"/><br>
<input type= "Submit" value= "Submission"/>
<input type= "reset" value= "reset"/>
</form>
</body>

<!--JSP page shows the basic UI, the action here, and then the button action, called the Dogetdemo servlet
The servlet performs two actions, gets the form submission data for the JSP page, and then executes the action, Outprint
-

3.web.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "3.0"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">
<display-name>archetype Created Web application</display-name>
<servlet>
<description>this is the description of my EE component</description>
<display-name>this is the display name of my EE component</display-name>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.test.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
<servlet>
<!--corresponding Javaservlet name---
<servlet-name>doGetDemo</servlet-name>
<servlet-class>com.test.doGetDemo</servlet-class>
</servlet>
<servlet-mapping>
<!--mapped servlet name--
<servlet-name>doGetDemo</servlet-name>
<!--what is Urlpattern?
The URL name of the servlet, and the path of the access servlet, the final path/.../name,name must be the same as the servlet's name--
<url-pattern>/doGetDemo</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>doPostDemo</servlet-name>
<servlet-class>com.test.doPostDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>doPostDemo</servlet-name>
<url-pattern>/doPostDemo</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FileRead</servlet-name>
<servlet-class>com.test.FileRead</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileRead</servlet-name>
<url-pattern>/FileRead</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

File class

public class Filetest{public static void Main (string[] args) throws ioexception{//creates a file object with the current path//Create a new file object, this object, Represents a path, "." is the relative path to the current directory. The file class, which uses a path string of files to create a document instance, either an absolute path or a relative path to file File = new file ("."); /Get the file name directly, output a little System.out.println (File.getname ()),//Get the parent path of the relative path may be error, the following code output NULLSYSTEM.OUT.PRINTLN (File.getparent () );//Get absolute path//The path here is the address where the Java virtual machine is running, and the output is Tomcat's Bin directory address System.out.println (File.getabsolutefile ());// Gets the upper-level path System.out.println (File.getabsolutefile (). GetParent ());//Create a temporary file under the current path//Create a file now really aaaxxxxxxxxx.txt// File Tmpfile = file.createtempfile ("AAA", ". txt", File);}}

File's Exits () method

File File = new file ("."); System.out.println (File.exists ());//True   the current path exists and returns true file File1=new file ("Test.txt"); System.out.println (File1.exists ());//true current path has test.txt file, return truefile file2=new file ("test"); System.out.println (File2.exists ());//False  the current path has no test file and returns falsefile File3=new file ("Content.txt"); System.out.println (File3.exists ());//false  The current path has no content.txt, return falsefile file4=new File ("d:\\ Apache-tomcat-9.0.0.m26\\bin\\content.txt "); System.out.println (File4.exists ());//true  the absolute path has content.txt return True

  

Ideaj+maven+javaweb Practice: Sevlet Implement Upload&download,javaio code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.