package cn.edu.sdu.service.common;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
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.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import cn.edu.sdu.framework.common.MyProperties;
import cn.edu.sdu.service.course.rule.Ppt2Html;
import cn.edu.sdu.service.course.rule.WordTOhtm;
public class SaveServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 2168411295049738216L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse arg1) throws ServletException, IOException {
/**
*/
String uploadSign = (String)req.getParameter("upload");
String rootPath="";
if(uploadSign==null||"".equals(uploadSign)){
arg1.getOutputStream().println("false");//上传失败
return ;
}
if("1".equals(uploadSign)){
rootPath = req.getSession().getServletContext().getRealPath("/");
File dirFile1 = new File(rootPath + "upload/");
boolean bfile1 = dirFile1.exists();
if(!bfile1){
System.out.println("upload,正在创建...");
bfile1 = dirFile1.mkdir();
if (bfile1 == true) {
System.out.println("创建成功!");
} else {
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}else {
if(!dirFile1.isDirectory()){//存在但不是文件夹
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}
File dirFile = new File(rootPath+ "upload/fileUpload/");
boolean bFile = dirFile.exists();
if (!bFile) {
System.out.println("fileUpload,正在创建...");
bFile = dirFile.mkdir();
if (bFile == true) {
System.out.println("创建成功!");
} else {
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}else {
if(!dirFile.isDirectory()){//存在但不是文件夹
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}
rootPath=rootPath+"upload/fileUpload/";
}else if ("2".equals(uploadSign)){
rootPath = req.getSession().getServletContext().getRealPath("/");
File dirFile1 = new File(rootPath + "upload/");
boolean bfile1 = dirFile1.exists();
if(!bfile1){
System.out.println("upload,正在创建...");
bfile1 = dirFile1.mkdir();
if (bfile1 == true) {
System.out.println("创建成功!");
} else {
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}else {
if(!dirFile1.isDirectory()){//存在但不是文件夹
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}
File dirFile = new File(rootPath+ "upload/attachement/");
boolean bFile = dirFile.exists();
if (!bFile) {
System.out.println("attachement,正在创建...");
bFile = dirFile.mkdir();
if (bFile == true) {
System.out.println("创建成功!");
} else {
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}else {
if(!dirFile.isDirectory()){//存在但不是文件夹
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}
rootPath=rootPath+"upload/attachement/";
}else if ("3".equals(uploadSign)){
rootPath = req.getSession().getServletContext().getRealPath("/");
File dirFile1 = new File(rootPath + "upload/");
boolean bfile1 = dirFile1.exists();
if(!bfile1){
System.out.println("upload,正在创建...");
bfile1 = dirFile1.mkdir();
if (bfile1 == true) {
System.out.println("创建成功!");
} else {
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}else {
if(!dirFile1.isDirectory()){//存在但不是文件夹
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}
File dirFile = new File(rootPath+ "upload/forumAttachement/");
boolean bFile = dirFile.exists();
if (!bFile) {
System.out.println("forumAttachement,正在创建...");
bFile = dirFile.mkdir();
if (bFile == true) {
System.out.println("创建成功!");
} else {
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}else {
if(!dirFile.isDirectory()){//存在但不是文件夹
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}
rootPath=rootPath+"upload/forumAttachement/";
}else if ("4".equals(uploadSign)){
MyProperties proper = new MyProperties("course.ftp.properties");
rootPath = proper.getProperty("pathRoot");
File dirFile = new File(rootPath);
boolean bFile = dirFile.exists();
if (!bFile) {
System.out.println(rootPath+",正在创建...");
bFile = dirFile.mkdir();
if (bFile == true) {
System.out.println("创建成功!");
} else {
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}else {
if(!dirFile.isDirectory()){//存在但不是文件夹
System.out.println("创建失败....!");
arg1.getOutputStream().println("false");
return ;
}
}
}
String name="";
//上传操作
if(null != uploadSign && !"".equals(uploadSign)){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
try {
Thread.sleep(1200);//使之不会产生在同一秒钟上传两个文件
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try{
List items = upload.parseRequest(req);
if(null != items){
Iterator itr = items.iterator();
while(itr.hasNext()){
FileItem item = (FileItem)itr.next();
if(item.isFormField()){
continue;
}else{
String filetype = item.getName().substring(item.getName().lastIndexOf("."));
if(item.getName().indexOf(",")>=0){
System.out.println("存在逗号");
arg1.getOutputStream().println("false");
return;
}
//以当前精确到秒的日期为上传的文件的文件名
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddkkmmss");
File savedFile = new File(rootPath,sdf.format(new Date())+item.getName());
if(filetype.equals(".wmv")||filetype.equals(".mp3")||filetype.equals(".wma")){
savedFile=new File(rootPath,sdf.format(new Date())+item.getName().substring(item.getName().length()-4));
}
System.out.println("上传的文件名为:"+savedFile.getName());
name=savedFile.getName();
item.write(savedFile);
}
}
}
}catch(Exception e){
arg1.getOutputStream().println("false");
e.printSt