Java File Upload function

Source: Internet
Author: User

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

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.