protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = null;
try {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
out = response.getWriter();
String receive_path="/files/"+UUID.randomUUID().toString() + "/";
String suffixs =".xls.pdf.doc.docx.xlsx";
String filepath=receiveFile( request,10, receive_path, suffixs );
System.out.println("上传文件所在路径:"+receive_path);
out.println(" 上传成功 ");
} catch (Exception e) {
e.printStackTrace();
out.println("上传失败! " );
} finally {
if (out != null) {
out.close();
}
}
}
public static String receiveFile(HttpServletRequest request,double MAX_SIZE,String receive_path,String suffixs ) throws AppException, UnsupportedEncodingException, FileUploadException {
// 获取表单信息
List
java.util.Iterator
String filePath ="";//导入接收文件全路径
while (iter.hasNext()) {
FileItem fi = iter.next();
if (!fi.isFormField()) {//
String filenamehz = fi.getName();// 文件名称
if ("".equals(filenamehz) || filenamehz == null) {
continue;
}
validateFileSize(fi.getSize(),MAX_SIZE, filenamehz);
validateFileHz( filenamehz,suffixs );
validataFileExists(receive_path);
filePath = receive_path +"/"+ filenamehz ;
filePath=validataFilePath( filePath) ;
System.out.println("导入接收文件临时全路径:"+filePath);
receiveSaveFile( fi, filePath);
}
}
if(filePath==null||filePath.equals("")) {
throw new AppException("没有检测到文件!文件接收失败!");
}
return filePath;
}
public static int validateFileSize(long filesize,double MAX_SIZE,String filename) throws AppException {
double size=getFileSize( filesize);
System.out.println("文件大小:"+size+"MB"+" 限制大小:"+MAX_SIZE+"MB");
if (MAX_SIZE>0&&size> MAX_SIZE) {
throw new AppException(" 文件:" + filename + "的尺寸超过"+MAX_SIZE+"MB,不能上传! ");
}
return 1;
}
public static double getFileSize(long filesize) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSize = df.format((double) filesize/ 1048576);// + "MB"
if(fileSize==null||fileSize.trim().equals("")) {
fileSize="0";
}
return Double.parseDouble(fileSize);
}
public static int validateFileHz(String filenamehz,String filetype) throws AppException {
if(filetype==null||"".equals(filetype)) {
return 1;
}
String houzhui = filenamehz.substring(filenamehz.lastIndexOf(".") + 1,filenamehz.length());
System.out.println("文件后缀:"+houzhui);
if(filetype.indexOf(houzhui.toLowerCase())==-1) {
throw new AppException("文件格式异常,请重新上传!(可上传文件后缀包括:"+filetype+")");
}
return 1;
}
public static String validataFilePath(String filePath) {
filePath=filePath.replace("\", "/");
filePath=filePath.replace("//", "/");
return filePath;
}
public static int validataFileExists(String pathdir) {
File file = new File( pathdir);
if (!file.exists() ) {// 如果文件夹不存在则创建
file.mkdirs();
}
return 1;
}
public static int receiveSaveFile(FileItem fi,String filePath) throws AppException {
try {
fi.write(new File(filePath));
fi.getOutputStream().flush();
fi.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
try {
if(fi!=null&&fi.getOutputStream()!=null) {
fi.getOutputStream().close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
throw new AppException(e.getMessage());
}
return 1;
}
public static List
request.setCharacterEncoding("GBK");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload uploader = new ServletFileUpload(factory);
List
return fileItems;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)