protected String uploadImage(ActionForm form, HttpServletRequest request) {
Hashtable ff = form.getMultipartRequestHandler().getFileElements();
String path = "";
Iterator<String> its = ff.keySet().iterator();
while (its.hasNext()) {
String key = its.next();
FormFile formfile = (FormFile) ff.get(key);
try {
path = this.uploadImage(request, formfile);
if (StringUtils.isBlank(path)) {
continue;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return path;
}
protected String uploadImage(HttpServletRequest request, FormFile file)
throws FileNotFoundException, IOException {
if (file == null || file.getFileSize() == 0) {
return "";
}
UploadManager umanager = new UploadManagerImpl();
String filePathInDir = umanager.upLoadFile(file,
UploadPathUtil.getPhysicPath(request));
file.destroy();
return UploadPathUtil.getContextPath(request) + filePathInDir;
}
public String upLoadFile(FormFile file, String uploadDir)
throws FileNotFoundException, IOException {
if (log.isDebugEnabled()) {
log.debug("===================================");
}
/**
* 如果文件不存在 返回null
*/
if (file == null || file.getFileSize() <= 0) {
if (log.isDebugEnabled()) {
log.debug("文件不存在 返回null");
}
return null;
}
/**
* 判断文件类型
*/
// String contenttype = file.getContentType();
// if (!StringUtils.contains(contenttype, "image")) {
// log.error("上传文件非法类型:" + contenttype);
// return null;
// }
String fileName = file.getFileName();
int index = fileName.indexOf(".");
String suffix = fileName.substring(index, fileName.length());
File dirPath = new File(uploadDir);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
// retrieve the file data
InputStream stream = file.getInputStream();
// write the file to the file specified
num++;
String pathInDir = System.currentTimeMillis() + "_" + num
+ suffix.toLowerCase();
/**
* if (ValidateUtil.isChineseExist(fileName)) { fileName = "1." +
* StringUtils.substringAfterLast(fileName, "."); } String pathInDir =
* System.currentTimeMillis() + "_" + fileName;
*/
String filePath = uploadDir + pathInDir;
OutputStream bos = new FileOutputStream(filePath);
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();
return pathInDir;
}
分享到:
相关推荐
用户struts1.2实现,经过测试了的。决对没问题。添加struts支持就可以了。不用再导其它JAR包
struts1上传图片,保存数据库,再显示jsp页面
struts实现图片上传功能。包含详细的代码和配置信息。
struts1下的上传下载源代码 注释清晰 上手容易,struts1下的上传下载struts1下的上传下载struts1下的上传下载struts1下的上传下载
图片上传成功实例java+struts1,试过成功了。swfupload的,很久以前网上当的一个例子,现在保存一下,大家需要的话可以试试。
实现图片上传图片预览,主要技术是struts1
NULL 博文链接:https://happypigs.iteye.com/blog/952382
使用struts2 上传图片到项目webapps目录下 然后生成缩略图 返回到页面显示缩略图
strut 上传图片 网站统计访问量 struts 重复提交
使用struts框架实现图片上传功能和显示图片。使用struts框架实现图片上传功能和显示图片。
struts2上传图片到Oracle,并进行显示,测试可以成功上传
ckeditor和struts2实现图片上传和编辑器上浏览!
使用struts2 上传图片,上传图片后生成一张压缩后的图片,和生成一张小图片,在网页上不用显示大图片,显示的缩小后的图片
struts上传视频与照片可以再后页面显示。支持同时上传。
Struts2上传图片并生成等比例缩略图的实例,在上传文件的同时生成等比例的缩略图,分放在不同的文件夹下。自己亲测,没有问题。
struts2.0图片上传,文件的配置。struts2。0的学习。io的技术。
struts2 上传图片限制.struts2 上传图片限制.struts2 上传图片限制.
struts下上传图片到数据库并显示到页面实例,简单明了
struts图片上传与下载,sql数据库
Jquery+struts2上传图片,制作进度条等示例,还有Jquery+Json+Struts实现Ajax技术,还有图形处理技术等,虽然很少,但都有重点突出,由于我也是学习,所以代码不是很简洁,对想学习交流的朋友非常合适