UploadphotoAction.java 上传图片

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");

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值