页面:
<script type="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;
}
}
<form action="demo" method="post" enctype="multipart/form-data">
<input type="file" name="myfile" id="file" />
<input type="submit" value="上传" onclick="return check()" />
</form>
Struts.xml:
<constant name="struts.multipart.maxSize" value="999999999"/>
<action name="demo" class="control.FileUploadAction" method="fileupload">
<param name="savePath">/upload</param>
<result name="success">/images.jsp</result>
<result name="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);
}
}