Java image Upload/File upload __java

Source: Internet
Author: User

Picture upload/File upload


1. Creating a Web project create a new JSP

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


<title>my JSP ' index.jsp ' starting page</title>

<body>
<form action= "<%=request.getcontextpath ()%>/upload.action" method= "post" enctype= "Multipart/form-data" >
Please select the uploaded file/Picture:
<input type= "File" name= "file"/>
<!--Please choose the uploaded Picture:-->
<!--<input type= "file" name= "file" accept= "image/*"/>-->
<input type= "Submit" value= "Upload"/>
</form>
</body>


2 Web.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.5"
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_2_5.xsd ">
<display-name></display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

</web-app>

3 Applicationcontext.xml


4 Springmvc.xml

<beans>

<!--is very important-->
<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name= "defaultencoding" value= "Utf-8" ></property>
</bean>

</beans>


5 Controller:

Package Com.bwie.controller;


Import Java.io.File;
Import java.io.IOException;
Import Java.util.UUID;


Import Javax.servlet.http.HttpServletRequest;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.multipart.MultipartFile;
Import Org.springframework.web.multipart.MultipartHttpServletRequest;



@Controller
public class Powercontroller {

@RequestMapping ("Upload")
public void upload (HttpServletRequest request) {
Get support File upload Request Object Multiparthttpservletrequest
Multiparthttpservletrequest mtpreq = (multiparthttpservletrequest) request;
Getting files in a file field through Mtpreq
Multipartfile file = mtpreq.getfile ("file");
Get the file's original file name through the Multipartfile object
String fileName = File.getoriginalfilename ();
Generate filename for a UUID
UUID Randomuuid = Uuid.randomuuid ();
Get the suffix name of the file
int i = Filename.lastindexof (".");
String uuidname = randomuuid.tostring () +filename.substring (i);
Gets the path address of the server (the saved address of the uploaded file)
String Realpath = Request.getsession (). Getservletcontext (). Getrealpath ("/file");
Convert a path to a folder and determine if the folder exists
File dir = new file (Realpath);
if (!dir.exists ()) {
Dir.mkdir ();
}
Gets the save path for a file
String Path = realpath+ "/" +uuidname;


Create a new space for the file in this server, * No data
File NewFile = new file (path);

try {
File.transferto (new File (path));
catch (IllegalStateException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

SYSTEM.ERR.PRINTLN ("-----Server's path address is::" +realpath);
SYSTEM.ERR.PRINTLN ("-----Picture name is::" +filename);
SYSTEM.ERR.PRINTLN ("-----Picture new name is::" +uuidname);
SYSTEM.ERR.PRINTLN ("-----Picture New path is::" +path);

}
}




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.