用struts实现单个文件上传并改变上传图片的大小
2013-09-09 00:00
225 查看
页面:
<scripttype="text/javascript"src="<%=path %>/crm/js/jquery-1.8.0.js"></script>
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
function check(e){
var filepath=$("input[id='file']").val();
var extStart=filepath.lastIndexOf(".");
var ext=filepath.substring(extStart,filepath.length).toUpperCase();
if(filepath==""){
alert("请选择需要上传的图片");
return false;
}else{
if(ext!=".BMP"&&ext!=".PNG"&&ext!=".GIF"&&ext!=".JPG"&&ext!=".JPEG"){
alert("图片限于bmp,png,gif,jpeg,jpg格式");
return false;
}
return true;
}
}
<formaction="demo"method="post"enctype="multipart/form-data">
<inputtype="file"name="myfile" id="file"/>
<inputtype="submit"value="上传"onclick="return check()"/>
</form>
Struts.xml:
<constantname="struts.multipart.maxSize"value="999999999"/>
<actionname="demo"class="control.FileUploadAction"method="fileupload">
<paramname="savePath">/upload</param>
<resultname="success">/images.jsp</result>
<resultname="input">/index.jsp</result>
</action>
FileUploadAction.java:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private static final int BUFFER_SIZE =800* 1024;//一次性可读最大字节
private File myfile;//上传文件域对象
private String myfileContentType;//文件类型
private String myfileFileName;//上传文件名
private String savePath;//保存路径
public String fileupload() {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
//以当前时间重新命名
String filename=df.format(new Date())+getExtention(this.getMyfileFileName());
//目标文件路径
String dstPath=ServletActionContext.getServletContext().getRealPath(this.getSavePath())+"\\"+filename;
File dstFile=new File(dstPath);
//读入
if (this.upload.length() > 2097152) {
setMessage("上传图片不能超过2M!");
setCustomerid(getCustomerid());
return lmzupload();
} else {
//改变上传图片的大小
Image image=ImageIO.read(this.upload);//构造Image对象
/*int width=image.getWidth(null);//得到源图宽
int height=image.getHeight(null);//得到源图高*/
BufferedImage bufferedImage=new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
//绘制缩小后的图片
bufferedImage.getGraphics().drawImage(image,0,0,500,500,null);
//临时图片
File tempfile=new File(ServletActionContext.getServletContext()
.getRealPath("crm")
+ "\\"
+ "customer"
+ "\\"+"temp.jpg");
FileOutputStream fos=new FileOutputStream(tempfile);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bufferedImage);//近JEEG编码
fos.close();
copy(this.myfile,dstFile);
}
setMyfileFileName(filename);
return SUCCESS;
}
//复制
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);//输入流
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);//输出流
byte[] buffer = new byte[BUFFER_SIZE];
//一次性读最大字节并写入
while (in.read(buffer) > 0) {
out.write(buffer);
}
}finally {
if (null != in) {
in.close();//关闭输入流
}
if (null != out) {
out.close();//关闭输出流
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//截取后缀名
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
}
<scripttype="text/javascript"src="<%=path %>/crm/js/jquery-1.8.0.js"></script>
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
function check(e){
var filepath=$("input[id='file']").val();
var extStart=filepath.lastIndexOf(".");
var ext=filepath.substring(extStart,filepath.length).toUpperCase();
if(filepath==""){
alert("请选择需要上传的图片");
return false;
}else{
if(ext!=".BMP"&&ext!=".PNG"&&ext!=".GIF"&&ext!=".JPG"&&ext!=".JPEG"){
alert("图片限于bmp,png,gif,jpeg,jpg格式");
return false;
}
return true;
}
}
<formaction="demo"method="post"enctype="multipart/form-data">
<inputtype="file"name="myfile" id="file"/>
<inputtype="submit"value="上传"onclick="return check()"/>
</form>
Struts.xml:
<constantname="struts.multipart.maxSize"value="999999999"/>
<actionname="demo"class="control.FileUploadAction"method="fileupload">
<paramname="savePath">/upload</param>
<resultname="success">/images.jsp</result>
<resultname="input">/index.jsp</result>
</action>
FileUploadAction.java:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private static final int BUFFER_SIZE =800* 1024;//一次性可读最大字节
private File myfile;//上传文件域对象
private String myfileContentType;//文件类型
private String myfileFileName;//上传文件名
private String savePath;//保存路径
public String fileupload() {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
//以当前时间重新命名
String filename=df.format(new Date())+getExtention(this.getMyfileFileName());
//目标文件路径
String dstPath=ServletActionContext.getServletContext().getRealPath(this.getSavePath())+"\\"+filename;
File dstFile=new File(dstPath);
//读入
if (this.upload.length() > 2097152) {
setMessage("上传图片不能超过2M!");
setCustomerid(getCustomerid());
return lmzupload();
} else {
//改变上传图片的大小
Image image=ImageIO.read(this.upload);//构造Image对象
/*int width=image.getWidth(null);//得到源图宽
int height=image.getHeight(null);//得到源图高*/
BufferedImage bufferedImage=new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
//绘制缩小后的图片
bufferedImage.getGraphics().drawImage(image,0,0,500,500,null);
//临时图片
File tempfile=new File(ServletActionContext.getServletContext()
.getRealPath("crm")
+ "\\"
+ "customer"
+ "\\"+"temp.jpg");
FileOutputStream fos=new FileOutputStream(tempfile);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bufferedImage);//近JEEG编码
fos.close();
copy(this.myfile,dstFile);
}
setMyfileFileName(filename);
return SUCCESS;
}
//复制
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);//输入流
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);//输出流
byte[] buffer = new byte[BUFFER_SIZE];
//一次性读最大字节并写入
while (in.read(buffer) > 0) {
out.write(buffer);
}
}finally {
if (null != in) {
in.close();//关闭输入流
}
if (null != out) {
out.close();//关闭输出流
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//截取后缀名
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
}
相关文章推荐
- ASP.NET上传图片时改变图片的尺寸和文件大小
- (下节)Spring+JDBC+Struts;实现图片上传(后台文件名要规范),加国际化;头文件basepath确保路径从webroot开始;
- Struts实现单文件上传、修改、删除(以图片为例)
- struts上传多文件,限制单个文件大小及总大小
- 使用Struts实现文件上传,格式限制,大小限制
- Struts实现单文件上传、修改、删除(以图片为例)
- Struts2实现图片上传功能 (单个图片文件)
- Struts 2 实现文件(单个或多个)的上传
- Struts 2实现文件的上传下载(解除文件大小限制)
- Struts实现单文件上传、修改、删除(以图片为例)
- PHP设置图片文件上传大小的具体实现方法
- Struts学习总结(五)---使用Struts2实现单个文件上传
- js实现上传图片本地预览功能以及限制图片的文件大小和尺寸大小
- 通过formFile实现上传文件,图片,或者其他,struts中继承action和dispatchAction的区别和用法
- PHP设置图片文件上传大小的具体实现方法
- PHP设置图片文件上传大小的具体实现方法
- 在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片
- js实现图片文件校验,验证上传的文件是图片和其后缀名,大小
- 基于jquery实现的上传图片及图片大小验证、图片预览效果代码!
- php实现上传图片文件代码