资源简介
基于struts上传头像功能,采用showModalDialog进行窗口弹出上传,关闭窗口实时更新头像,有需要的请猛击下载
代码片段和文件信息
package net.blogjava.upload;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class UploadImageAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping ActionForm form
HttpServletRequest request HttpServletResponse response)
throws Exception {
UploadImageForm uploadForm = (UploadImageForm) form;
FormFile image = uploadForm.getFormFile();
if (image.getFileSize() == 0) {
request.setAttribute(“msg“ “你还没选择文件呢!“);
return mapping.findForward(“input“);
}
if (!checkImageContentType(image)) {
request.setAttribute(“msg“ “图片类型不正确,请重新选择!“);
return mapping.findForward(“input“);
}
String path = servlet.getServletContext().getRealPath(“/upload/image“);
path += “/“;
String photoName = “superxzl“;
photoName = photoName
+ image.getFileName().substring(
image.getFileName().lastIndexOf(“.“));
System.out.println(“[PhotoName]“ + photoName);
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
try {
InputStream stream = image.getInputStream();
OutputStream bos = new FileOutputStream(path + photoName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer 0 8192)) != -1) {
bos.write(buffer 0 bytesRead);
}
bos.close();
stream.close();
} catch (FileNotFoundException fnfe) {
request.setAttribute(“msg“ “文件路径不正确,请重新选择!“);
return mapping.findForward(“input“);
} catch (IOException ioe) {
request.setAttribute(“msg“ “上传出错,请重来!“);
return mapping.findForward(“input“);
}
image.destroy();
System.out.println(“[Photo_Src]“
+ servlet.getServletContext().getContextPath() + “/image/“
+ image.getFileName());
// String photoPath =
// servlet.getServletContext().getContextPath()+“/image/“ +
// image.getFileName();
request.setAttribute(“fileName“ photoName);
request.setAttribute(“msg“ “success“);
return mapping.findForward(“input“);
}
private boolean checkImageContentType(FormFile formFile) {
String contentType = formFile.getContentType();
System.out.println(“[ContentType]“ + contentType);
if (contentType.equals(“image/pjpeg“)
|| contentType.equals(“image/jpeg“)
|| contentType.equals(“image/gif“)
|| contentType.equals(“image/bmp“)) {
return true;
}
return false;
}
}
- 上一篇:ultimate-member
- 下一篇:广东工业大学数字信号处理实验报告
相关资源
- python实现的ftp自动上传、下载脚本
- stm32 ds18b20 温度传感器 测试通过
- 上传大文件并显示进度条控件实例
- 通过webservice上传和下载文件
- 基于J2EE物资出入库管理系统
- 图片上传组件v1.0
- 关于Spring MVC项目maven中通过fileupload上
- cropper.js h5裁剪上传图片 代码齐整
- 仿QQ头像裁剪功能
- 电信物联网NB-lot上传编解码插件检测
- Cocos2d-x 3.x 头像选择器功能扩展Image
- Spring+Struts2+Mybatis的一个增删改查的
- JQ可拖曳上传图片插件兼容手机
- maven+ssm框架视频上传预览.zip
- Struts2与Ueditor整合SSH+Ueditor
- OSS图片上传 swift
- Struts2漏洞检查工具2018版 V2.1.exe
- 毕设-云视频学习平台的设计及实现
- uploadify上传插件完整Demo包括后台
- Struts2表单标签使用范例
- IIS+SVN实现本地上传服务器自动更新
- .net百度编辑器UEditor)上传图片跟上次
- Struts2+Hiberntate+Spring档案管理系统数据
- spring+struts2+mybatis三大框架集成实现用
- 基于Hibernate与Struts2框架的整合项目之
- Qt之FTP客户端
- 单片机通过ESP8266上传温湿度数据
- 《Struts 2.x权威指南(第3版)》.(李
- jfinal包括登录注册留言修改上传文件
- .net MVC+Bootstrap下使用localResizeIMG上传图
评论
共有 条评论