package cn.itcast.web.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
public class Upload2Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.创建 DiskFileItemFactory
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2.创建
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 底层通过request获取数据,进行解析,将解析的数据封装到List<FileItem>
List<FileItem> items = upload.parseRequest(request);
// 3.遍历集合
for (FileItem item : items) {
// 判断该数据项是否为文件上传项,true 不是文件上传 false 是文件上传
if (item.isFormField()) {
// 这就是得到了<input type="text" name="content">这样的组件
String fieldName = item.getFieldName(); // 获取组件name属性值
System.out.println(fieldName);
String name = item.getName(); // null
System.out.println(name);
String msg = item.getString(); // 获取组件内容
System.out.println(msg);
} else {
// 这就是得到了<input type="file" name="f">这样的组件.
String fieldName = item.getFieldName(); // 获取组件name属性值
System.out.println("上传组件的名称:" + fieldName);
String name = item.getName(); // 上传文件名称(全路径)
System.out.println(name);
name = name.substring(name.lastIndexOf("\\") + 1);
System.out.println("上传文件的名称:" + name);
String msg = item.getString(); // 获取上传文件内容
System.out.println(msg);
// 获取上传文件内容,完成文件 上传操作
InputStream is = item.getInputStream();
byte[] b = new byte[1024];
int len = -1;
FileOutputStream fos = new FileOutputStream("d:/upload/"
+ name);
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);
}
fos.close();
is.close();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 方法二
// 1.创建 DiskFileItemFactory
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2.创建
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 底层通过request获取数据,进行解析,将解析的数据封装到List<FileItem>
List<FileItem> items = upload.parseRequest(request);
// 3.遍历集合
for (FileItem item : items) {
// 判断该数据项是否为文件上传项,true 不是文件上传 false 是文件上传
if (item.isFormField()) {
// 这就是得到了<input type="text" name="content">这样的组件
String fieldName = item.getFieldName(); // 获取组件name属性值
String msg = item.getString(); // 获取组件内容
} else {
// 这就是得到了<input type="file" name="f">这样的组件.
String name = item.getName(); // 上传文件名称(全路径)
name = name.substring(name.lastIndexOf("\\") + 1);
// 获取上传文件内容,完成文件 上传操作
FileOutputStream fos = new FileOutputStream("d:/upload/"
+ name);
IOUtils.copy(item.getInputStream(), fos);
fos.close();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
package cn.itcast.utils;
import java.io.File;
import java.util.UUID;
public class FileUploadUtils {
// 得到上传文件真实名称 c:\a.txt a.txt
public static String getRealName(String filename) {
int index = filename.lastIndexOf("\\") + 1;
return filename.substring(index);
}
// 获取随机名称 a.txt
public static String getUUIDFileName(String filename) {
int index = filename.lastIndexOf(".");
if (index != -1) {
return UUID.randomUUID() + filename.substring(index);
} else {
return UUID.randomUUID().toString();
}
}
// 目录分离算法
public static String getRandomDirectory(String filename) {
//方法一
// int hashcode = filename.hashCode();
//
// // System.out.println(hashcode);
//
// // int类型数据在内存中占32位。转换成16进制数,就得到8个16进制数
// String hex = Integer.toHexString(hashcode);
//
// // System.out.println(hex); // 056d9363
//
// return "/" + hex.charAt(0) + "/" + hex.charAt(1);
//方法二
int hashcode = filename.hashCode();
System.out.println(Integer.toBinaryString(hashcode));
int a = hashcode & 0xf;
hashcode = hashcode >>> 4;
int b = hashcode & 0xf;
return "/" + a + "/" + b;
}
}