Jsp
<%@ page language="java" import="java.util.*" pageencoding= "UTF-8"%>
<%String path = request. Getcontextpath (); String basepath = request. Getscheme () +"://"+request. getServerName () +": +request. Getserverport () +path+"/";%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ' index.jsp ' starting page</title>
<meta http-equiv="Pragma" content="No-cache">
<meta http-equiv="Cache-control" content="No-cache">
<meta http-equiv="Expires" content="0">
<meta http-equiv="keywords" content="Keyword1,keyword2,keyword3" >
<meta http-equiv="description" content="This is my page" >
<!--<link rel= "stylesheet" type= "Text/css" href= "Styles.css" > -
</head>
<body>
<form Action="${pagecontext.request.contextpath}/upload3" method= "POST" enctype="Multipart/form-data">
User name;<input type="text" name="username"/><br/>
Photos<input type="file" name="Zhaopian"/><br/>
<input type="Submit" value="Upload"/>
</form>
</body>
</html>
Servlet
Packagecom. Kero99. YGC. Test;
Import Java. IO. File;
Import Java. IO. IOException;
Import Java. Util. List;
Import Java. Util. UUID;Import Javax. servlet. Servletexception;
Import Javax. servlet. HTTP. HttpServlet;
Import Javax. servlet. HTTP. HttpServletRequest;
Import Javax. servlet. HTTP. HttpServletResponse;
import org. Apache. Commons. FileUpload. Fileitem;
import org. Apache. Commons. FileUpload. Disk. Diskfileitemfactory;
import org. Apache. Commons. FileUpload. servlet. Servletfileupload;
import org. Apache. Commons. IO. Filenameutils;
Importcom. Sun. CORBA. SE. SPI. Legacy. Connection. Getendpointinfoagainexception;
public class Upload extends httpservlet{
@Override
protected void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
DoPost (Request, response);
}
@Override
protected void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Setting the Response browser format
Response. setcharacterencoding("Utf-8");
Response. setContentType("Text/html;charset=utf-8");
String Savapath=getservletcontext (). Getrealpath("/web-inf/files");
Diskfileitemfactory diskfileitemfactory=new diskfileitemfactory ();
Servletfileupload fileupload=new servletfileupload (diskfileitemfactory);
try {
FileUpload. setheaderencoding("Utf-8");
List<fileitem> List=fileupload. Parserequest(Request);
for (Fileitem item:list) {
if (item. Isformfield()){
String Name=item. GetFieldName();//Get text items
String Value=item. getString("Utf-8");//Get text content
String Value1=new string (name. GetBytes("Iso8859-1"),"UTF-8");
}else{//for uploading is a file
String Filename1=item. GetName();
String Filename=uuid. Randomuuid(). toString(). Replace("-","") +filename1;
Directory Break Method A/b
String path=getfilechildpath (filename);
File File=new file (Savapath,path);
if (!file. Exists() &&!file. Isdirectory()){
File. Mkdirs();
String extension=filenameutils. GetExtension(filename);
if (! ("JSP". Equals(extension) | |"TXT". Equals(extension))) {
File File1=new file (file,filename);
Item. Write(File1);
Response. Getwriter(). Print("Success");
}else{
Response. Getwriter(). Print("file cannot be a JSP and EXE type");
}
}else{
Response. Getwriter(). Print("The file cannot be empty");
}
}
}
} catch (Exception e) {
E. Printstacktrace();
}
}
Catalog Break up
public string Getfilechildpath (string filename) {
int Hcode=filename. Hashcode();
String Hex=integer. tohexstring(Hcode);
String Path=hex. CharAt(0)+"/"+hex. CharAt(1);
return path;
}
@Override
public void Destroy () {
TODO auto-generated Method Stub
Super. Destroy();
}
@Override
public void Init () throws Servletexception {
TODO auto-generated Method Stub
Super. Init();
}
}
This article from "Avenue to Jane, Wu in Tiancheng" blog, please be sure to keep this source http://kero99.blog.51cto.com/12965144/1952200
Java File Upload function