jsp页面
<h2>注册表单</h2>
<form action="${pageContext.request.contextPath }/Register" method="post" enctype="multipart/form-data">
账号:<input type="text" name="username" value="葫芦娃" />
<br/>
密码:<input type="password" name="password" value="123456" />
<br/>
酷头:<input type="file" name="kuhead" />
<br/>
<input type="submit" value="注册" />
</form>
servlet 页面
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、编写固定实现DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(factory);//解析器
//2、解析request 流中的数据
try {
List<FileItem> fis = fileUpload.parseRequest(request);
for(FileItem fi : fis){
//根据 FileItem 进行分别处理
if(fi.isFormField()){//一般表单字段
//获取字段名
String key = fi.getFieldName();
//获取字段值
String value = new String(fi.getString().getBytes("ISO-8859-1"),"UTF-8");
System.out.println(key + " " + value);
} else {
//获取字段名
String key = fi.getFieldName();
//获取文件名,不需要进行乱码处理
String name = fi.getName();
System.out.println(key + " " + name);
//获取文件内容,转存为 磁盘 文件
//A、确定存储路径
ServletContext sc = request.getServletContext();
String fatherPath = sc.getRealPath("/img");
//加了子级目录,提高了查找效率
fatherPath += UploadUtils.getDir();
File father = new File(fatherPath);
father.mkdirs();
//B、文件名
String fileName = UUIDUtils.getUUID();
//C、后缀
int index = name.lastIndexOf(".");
String houZhui= name.substring(index);
fileName += houZhui;
//创建 file 对象
File file = new File(fatherPath + "/" + fileName);
file.createNewFile();
//获取上传的文件流,写出数据到 file
InputStream in = fi.getInputStream();
OutputStream out = new FileOutputStream(file);
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
UploadUtils工具类
public class UploadUtils {
/**
* 获取文件真实名称
* 由于浏览器的不同获取的名称可能为:c:/upload/1.jpg或者1.jpg
* 最终获取的为 1.jpg
* @param name 上传上来的文件名称
* @return 真实名称
*/
public static String getRealName(String name){
//获取最后一个"/"
int index = name.lastIndexOf("\\");
return name.substring(index+1);
}
/**
* 获取随机名称
* @param realName 真实名称
* @return uuid 随机名称
*/
public static String getUUIDName(String realName){
//realname 可能是 1.jpg 也可能是 1
//获取后缀名
int index = realName.lastIndexOf(".");
if(index==-1){
return UUID.randomUUID().toString().replace("-", "").toUpperCase();
}else{
return UUID.randomUUID().toString().replace("-", "").toUpperCase()+realName.substring(index);
}
}
/**
* 获取文件目录,可以获取256个随机目录
* @return 随机目录
*/
public static String getDir(){
String s="0123456789ABCDEF";
Random r = new Random();
return "/"+s.charAt(r.nextInt(16))+"/"+s.charAt(r.nextInt(16));
}
}
/**
* 获取文件真实名称
* 由于浏览器的不同获取的名称可能为:c:/upload/1.jpg或者1.jpg
* 最终获取的为 1.jpg
* @param name 上传上来的文件名称
* @return 真实名称
*/
public static String getRealName(String name){
//获取最后一个"/"
int index = name.lastIndexOf("\\");
return name.substring(index+1);
}
/**
* 获取随机名称
* @param realName 真实名称
* @return uuid 随机名称
*/
public static String getUUIDName(String realName){
//realname 可能是 1.jpg 也可能是 1
//获取后缀名
int index = realName.lastIndexOf(".");
if(index==-1){
return UUID.randomUUID().toString().replace("-", "").toUpperCase();
}else{
return UUID.randomUUID().toString().replace("-", "").toUpperCase()+realName.substring(index);
}
}
/**
* 获取文件目录,可以获取256个随机目录
* @return 随机目录
*/
public static String getDir(){
String s="0123456789ABCDEF";
Random r = new Random();
return "/"+s.charAt(r.nextInt(16))+"/"+s.charAt(r.nextInt(16));
}
}