Java简单上传图片例子
来源:互联网 发布:中银淘宝卡注销 编辑:程序博客网 时间:2024/06/09 19:03
前台页面:
<!--注意enctype表明设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作.-->
<form id="addform" method="post" enctype="multipart/form-data" action="${base}/action/addPhoPic">
<table border="0" class="perview" align="center">
<a href="#" onClick="toaddpic()">上传至相册</a>
<tr>
<th>选择文件</th>
<th width="50%">预览图</th>
</tr>
<tr>
<td height="200"><input id="idFile" name="upload" type="file" /></td>
<td align="center"><img id="idImg" /></td>
</tr>
</table>
</form>
Java后台处理:
//与前台页面空间name一致
private File upload;
//反射,得到文件类型,文件名称
private String uploadContentType;
private String uploadFileName;
public String doAddPhoPic(){
//自己的PhotoService接口处理
IPhotoService photoService=BeanFactory.getBean(BeanConstants.WB_PHOTO_SERVICE);
Photo photo=new Photo();
//这里简单的demo没有要把名字规范,也没有对图片有剪切或缩小处理,所以就简单的把上传的图片以1,2,3命名
int count=photoService.queryPhotoList().size();
count++;
String file_path1="";
String file_path2="";
try {
//上传至该项目所在本地目录
file_path1=Constants.BASE_ROOT+"/fullsize"+"/"+count+".jpg";
file_path2=Constants.BASE_ROOT+"/thumbs"+"/"+count+".jpg";
photo.setPicName(photoService.queryPhotoList().size()+1+".jpg");
photo.setPicUrl(file_path2);
photoService.insertPhoto(photo);
System.out.println("---"+file_path1);
System.out.println("---"+file_path2);
//对文件进行写操作
FileOutputStream fos1=new FileOutputStream(file_path1);
FileOutputStream fos2=new FileOutputStream(file_path2);
//对文件进行读操作
FileInputStream fis=new FileInputStream(upload);
byte[] buffer=new byte[1024];
int len=0;
//读入流,保存至byte数组
while((len=fis.read(buffer))>0){
fos1.write(buffer,0,len);
fos2.write(buffer,0,len);
}
fos1.close();
fos2.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
list=photoService.queryPhotoList();
return SUCCESS;
}
<!--注意enctype表明设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作.-->
<form id="addform" method="post" enctype="multipart/form-data" action="${base}/action/addPhoPic">
<table border="0" class="perview" align="center">
<a href="#" onClick="toaddpic()">上传至相册</a>
<tr>
<th>选择文件</th>
<th width="50%">预览图</th>
</tr>
<tr>
<td height="200"><input id="idFile" name="upload" type="file" /></td>
<td align="center"><img id="idImg" /></td>
</tr>
</table>
</form>
Java后台处理:
//与前台页面空间name一致
private File upload;
//反射,得到文件类型,文件名称
private String uploadContentType;
private String uploadFileName;
public String doAddPhoPic(){
//自己的PhotoService接口处理
IPhotoService photoService=BeanFactory.getBean(BeanConstants.WB_PHOTO_SERVICE);
Photo photo=new Photo();
//这里简单的demo没有要把名字规范,也没有对图片有剪切或缩小处理,所以就简单的把上传的图片以1,2,3命名
int count=photoService.queryPhotoList().size();
count++;
String file_path1="";
String file_path2="";
try {
//上传至该项目所在本地目录
file_path1=Constants.BASE_ROOT+"/fullsize"+"/"+count+".jpg";
file_path2=Constants.BASE_ROOT+"/thumbs"+"/"+count+".jpg";
photo.setPicName(photoService.queryPhotoList().size()+1+".jpg");
photo.setPicUrl(file_path2);
photoService.insertPhoto(photo);
System.out.println("---"+file_path1);
System.out.println("---"+file_path2);
//对文件进行写操作
FileOutputStream fos1=new FileOutputStream(file_path1);
FileOutputStream fos2=new FileOutputStream(file_path2);
//对文件进行读操作
FileInputStream fis=new FileInputStream(upload);
byte[] buffer=new byte[1024];
int len=0;
//读入流,保存至byte数组
while((len=fis.read(buffer))>0){
fos1.write(buffer,0,len);
fos2.write(buffer,0,len);
}
fos1.close();
fos2.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
list=photoService.queryPhotoList();
return SUCCESS;
}
- Java简单上传图片例子
- Java简单上传图片例子
- 图片上传(简单例子)
- iOS使用http post多图片上传带描述+java后台服务器,简单实现例子.
- java FTP 上传文件的简单例子
- 图片上传(加水印、缩略图、远程保存)的简单例子
- 图片上传(加水印、缩略图、远程保存)的简单例子
- 图片上传(加水印、缩略图、远程保存)的简单例子
- 用最简单的例子实现jQuery图片即时上传
- 用最简单的例子实现jQuery图片即时上传
- 一个简单的安卓+Servlet图片上传例子
- 用最简单的例子实现jQuery图片即时上传
- 图片上传的例子
- flex 上传图片 例子
- Django上传图片例子
- JAVA上传图片(简单版)
- 简单多图片上传 jquery+java 代码
- Java上传图片到七牛服务器简单教程
- struts servlet spring区别(整理)
- AWR-比较两个阶段性能
- 土耳其和突厥的历史与现状
- 新型视觉交互技术:把一张纸变成触摸屏(转载)
- struts和servlet的区别和联系
- Java简单上传图片例子
- Siri当心!谷歌正从苹果业务中分走一杯羹
- struts2的工作机制
- hbase笔记
- struts1 struts2 spring 各自的运行机制,工作流程(整理)
- 个人自制maven快速入门及其管理web项目多模块的依赖图文并茂教程
- 2013年中国40位40岁以下的商界精英(转载)
- struts2 拦截器 interceptor
- 办公室必备,健康生活:常见室内植物的功效,知道以后就别乱摆了