package com.hbsi.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import javax.activation.FileTypeMap;
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.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
private List fileType=Arrays.asList(".jsp",".txt");
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 文件上传
try {
// 创建一个解析器工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//临时文件
factory.setRepository(new File(this.getServletContext().getRealPath("/temp")));
// 得到解析器
ServletFileUpload upload = new ServletFileUpload(factory);
// 解决文件名为中文乱码的文件
upload.setHeaderEncoding("utf-8");
// 设置上传文件的最大值
upload.setFileSizeMax(1024 * 1024);
//监听器
upload.setProgressListener(new ProgressHandler());
// if(upload.isMultipartContent(request))
// 对相应的请求进行解析,有几个输入项,就有几个FileItem对象
List<FileItem> list = upload.parseRequest(request);
// 要迭代list集合,获取list集合中每一个输入项
for (FileItem item : list) {
// 判断输入的类型
String[] arr={".txt",".jsp"};
if (item.isFormField()) {
// 普通输入项
String inputName = item.getFieldName();
String inputValue = item.getString("utf-8");
// 解决中文用户名的乱码或者
// inputValue= new
// String(inputValue.getBytes("iso8859-1"),"utf-8");
System.out.println(inputName + "::" + inputValue);
} else {
// 上传文件输入项
String filename = item.getName();// 上传文件的文件名1.txt
// 系统找不到指定的路径异常的解决
if (!filename.trim().equals("")) {
filename = filename.substring(filename
.lastIndexOf("\\") + 1);
String ext=filename.substring(filename.lastIndexOf("."));
if(!fileType.contains(ext)){
request.setAttribute("message", "文件类型不对");
request.getRequestDispatcher("/message.jsp").forward(request, response);
return;
}
// 输入流
InputStream is = item.getInputStream();
// 输出
//保存文件在webroot中,不被保护,放到webInf中受保护
String savePath=this.getServletContext().getRealPath("WEB-INF/upload");
//在upload下建多级的子目录
savePath=generateFilePath(savePath, filename);
//对文件名加工
filename=UUID.randomUUID().toString()+"_"+filename;
//构建输出流
FileOutputStream fos = new FileOutputStream(savePath
+"\\"+ filename);
// 缓冲区
byte[] buff = new byte[1024];
// 设置缓冲长度
int len = 0;
// 设置循环语句读取
while ((len = is.read(buff)) > 0) {
fos.write(buff);
}
is.close();
fos.close();
// 临时文件的删除问题必须在关闭之后
item.delete();
}
}
}
request.setAttribute("message", "ok okokokokoko");
} catch(FileUploadBase.FileSizeLimitExceededException e){
request.setAttribute("message", "上传文件不能超过1M");
}catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("message", "nonononononono");
}
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
public String generateFilePath(String path,String filename){
//产生目录结构的算法:hash目录
int dir1=filename.hashCode() & 0x0f;//当成一个目录名,一级目录
int dir2=filename.hashCode()>>4 & 0x0f;//二级目录
String savePath=path+"\\"+dir1+"\\"+dir2+"\\";
//建目录结构
File f=new File(savePath);
if(!f.exists()){
f.mkdirs();
}
return savePath;
}
class ProgressHandler implements ProgressListener{
public void update(long arg0, long arg1, int arg2) {
//自动调用
// TODO Auto-generated method stub
System.out.println("现在已经处理了"+arg0+"数据,总迷糊数据量"+arg1+"增在处理"+arg2);
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import javax.activation.FileTypeMap;
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.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
private List fileType=Arrays.asList(".jsp",".txt");
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 文件上传
try {
// 创建一个解析器工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//临时文件
factory.setRepository(new File(this.getServletContext().getRealPath("/temp")));
// 得到解析器
ServletFileUpload upload = new ServletFileUpload(factory);
// 解决文件名为中文乱码的文件
upload.setHeaderEncoding("utf-8");
// 设置上传文件的最大值
upload.setFileSizeMax(1024 * 1024);
//监听器
upload.setProgressListener(new ProgressHandler());
// if(upload.isMultipartContent(request))
// 对相应的请求进行解析,有几个输入项,就有几个FileItem对象
List<FileItem> list = upload.parseRequest(request);
// 要迭代list集合,获取list集合中每一个输入项
for (FileItem item : list) {
// 判断输入的类型
String[] arr={".txt",".jsp"};
if (item.isFormField()) {
// 普通输入项
String inputName = item.getFieldName();
String inputValue = item.getString("utf-8");
// 解决中文用户名的乱码或者
// inputValue= new
// String(inputValue.getBytes("iso8859-1"),"utf-8");
System.out.println(inputName + "::" + inputValue);
} else {
// 上传文件输入项
String filename = item.getName();// 上传文件的文件名1.txt
// 系统找不到指定的路径异常的解决
if (!filename.trim().equals("")) {
filename = filename.substring(filename
.lastIndexOf("\\") + 1);
String ext=filename.substring(filename.lastIndexOf("."));
if(!fileType.contains(ext)){
request.setAttribute("message", "文件类型不对");
request.getRequestDispatcher("/message.jsp").forward(request, response);
return;
}
// 输入流
InputStream is = item.getInputStream();
// 输出
//保存文件在webroot中,不被保护,放到webInf中受保护
String savePath=this.getServletContext().getRealPath("WEB-INF/upload");
//在upload下建多级的子目录
savePath=generateFilePath(savePath, filename);
//对文件名加工
filename=UUID.randomUUID().toString()+"_"+filename;
//构建输出流
FileOutputStream fos = new FileOutputStream(savePath
+"\\"+ filename);
// 缓冲区
byte[] buff = new byte[1024];
// 设置缓冲长度
int len = 0;
// 设置循环语句读取
while ((len = is.read(buff)) > 0) {
fos.write(buff);
}
is.close();
fos.close();
// 临时文件的删除问题必须在关闭之后
item.delete();
}
}
}
request.setAttribute("message", "ok okokokokoko");
} catch(FileUploadBase.FileSizeLimitExceededException e){
request.setAttribute("message", "上传文件不能超过1M");
}catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("message", "nonononononono");
}
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
public String generateFilePath(String path,String filename){
//产生目录结构的算法:hash目录
int dir1=filename.hashCode() & 0x0f;//当成一个目录名,一级目录
int dir2=filename.hashCode()>>4 & 0x0f;//二级目录
String savePath=path+"\\"+dir1+"\\"+dir2+"\\";
//建目录结构
File f=new File(savePath);
if(!f.exists()){
f.mkdirs();
}
return savePath;
}
class ProgressHandler implements ProgressListener{
public void update(long arg0, long arg1, int arg2) {
//自动调用
// TODO Auto-generated method stub
System.out.println("现在已经处理了"+arg0+"数据,总迷糊数据量"+arg1+"增在处理"+arg2);
}
}
}