import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.sql.ResultSet;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import com.xueji.struts.actionform.PhotoActionForm;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class UploadphotoAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// init(request);
// if (!isLogin) {
// return mapping.findForward("relogin");
// }
// String flag = request.getParameter("flag");
// String loadpathto = request.getParameter("loadpathto");
// String filePath = request.getRealPath("/"); //取当前系统路径
// String pathphoto = loadpathto;
if (form instanceof PhotoActionForm) {// 如果form是PhotoActionForm
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
response.setContentType("text/html; charset=GBK");// 如果没有指定编码,编码格式为GBk
}
PhotoActionForm theForm = (PhotoActionForm) form;
FormFile file = theForm.getMyfile();// 取得上传的文件
try {
if (file.getFileSize() > (1024 * 1024)) {
request.setAttribute("message", "文件请控制在1M以下!");
return mapping.findForward("ErrorInfo");
}
String contentType = file.getContentType();
System.out.println("文件类别:=" + contentType);
String filebackstr = "";
if (contentType.equals("image/pjpeg")) {
filebackstr = ".jpg";
} else if (contentType.equals("image/gif")) {
filebackstr = ".gif";
} else if (contentType.equals("image/bmp")) {
filebackstr = ".bmp";
} else {
request.setAttribute("message", "文件格式不正确!");
return mapping.findForward("ErrorInfo");
}
String path ="E:/struts/xueji/WebRoot/image\\"+ file.getFileName();
if(file!=null){
System.out.println("fileName=" + file.getFileName());
System.out.println(path);
FileOutputStream fos = new FileOutputStream(path);
fos.write(file.getFileData());
fos.flush();
fos.close();
}
} catch (Exception e) {
System.err.print(e);
}
// String sql="update sync4j_user set MYFACEPATH='"
// +pathphoto.replaceFirst("/","") +userid+file.getFileName()+"'
// where userid="+userid;
// db.doSql(sql, db.DELETE_MODE, conn);
} else {
request.setAttribute("message", "上传有误");
return mapping.findForward("ErrorInfo");
}
request.setAttribute("msg", "上传成功");
return mapping.findForward("success");
}
}
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.sql.ResultSet;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import com.xueji.struts.actionform.PhotoActionForm;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class UploadphotoAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// init(request);
// if (!isLogin) {
// return mapping.findForward("relogin");
// }
// String flag = request.getParameter("flag");
// String loadpathto = request.getParameter("loadpathto");
// String filePath = request.getRealPath("/"); //取当前系统路径
// String pathphoto = loadpathto;
if (form instanceof PhotoActionForm) {// 如果form是PhotoActionForm
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
response.setContentType("text/html; charset=GBK");// 如果没有指定编码,编码格式为GBk
}
PhotoActionForm theForm = (PhotoActionForm) form;
FormFile file = theForm.getMyfile();// 取得上传的文件
try {
if (file.getFileSize() > (1024 * 1024)) {
request.setAttribute("message", "文件请控制在1M以下!");
return mapping.findForward("ErrorInfo");
}
String contentType = file.getContentType();
System.out.println("文件类别:=" + contentType);
String filebackstr = "";
if (contentType.equals("image/pjpeg")) {
filebackstr = ".jpg";
} else if (contentType.equals("image/gif")) {
filebackstr = ".gif";
} else if (contentType.equals("image/bmp")) {
filebackstr = ".bmp";
} else {
request.setAttribute("message", "文件格式不正确!");
return mapping.findForward("ErrorInfo");
}
String path ="E:/struts/xueji/WebRoot/image\\"+ file.getFileName();
if(file!=null){
System.out.println("fileName=" + file.getFileName());
System.out.println(path);
FileOutputStream fos = new FileOutputStream(path);
fos.write(file.getFileData());
fos.flush();
fos.close();
}
} catch (Exception e) {
System.err.print(e);
}
// String sql="update sync4j_user set MYFACEPATH='"
// +pathphoto.replaceFirst("/","") +userid+file.getFileName()+"'
// where userid="+userid;
// db.doSql(sql, db.DELETE_MODE, conn);
} else {
request.setAttribute("message", "上传有误");
return mapping.findForward("ErrorInfo");
}
request.setAttribute("msg", "上传成功");
return mapping.findForward("success");
}
}