📄 uploadaction.java
字号:
package action;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import formbean.UploadForm;
import beans.UserBean;
import beans.DataBean;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2006-6-23
* Time: 9:55:33
* To change this template use File | Settings | File Templates.
*/
public class UploadAction extends Action {
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=gbk");
UploadForm uploadForm = (UploadForm)form;
FormFile pic = uploadForm.getPic();
String name = new String(uploadForm.getName().getBytes("ISO-8859-1")) ;
String typecon = new String(uploadForm.getPictype() .getBytes("ISO-8859-1") );
String description = new String(uploadForm.getDescription() .getBytes("ISO-8859-1") );
if(!pic.equals("") && !name.equals("") && !typecon.equals("") && !description.equals("")){
String picname = pic.getFileName();
String fname=new String(picname.getBytes("ISO-8859-1"));
String d = request.getRealPath("pictures");
String uploadFileName = request.getRealPath("pictures")+"\\"+typecon+"\\"+fname;
File upliadFile = new File(uploadFileName);
BufferedInputStream bis = null;
String filepos = "pictures"+"\\"+typecon+"\\"+fname;
HttpSession session =request.getSession();
UserBean ub = (UserBean)session.getAttribute("usebean");
String sql = "insert into pictures (name,type,pos,detail,typecon,usename,localpos) values ('"+name+"','"+pic.getContentType()+"','"+filepos+"','"+description+"','"+typecon+"','"+ub.getUsername() +"','"+uploadFileName+"') ";
DataBean db = new DataBean();
Image image = null;
BufferedOutputStream bos = null;
try{
if(pic.getFileSize()<2*1024*1024){
bis = new BufferedInputStream(pic.getInputStream());
image = javax.imageio.ImageIO.read(bis);
int width = image.getWidth(null);
int height = image.getHeight(null);
int w = 160;
int h = 120;
if(width>w||height>h){
BufferedImage bi = new BufferedImage(w,h,
BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(image,0,0,w,h,null);
bos = new BufferedOutputStream(new FileOutputStream(
upliadFile));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(bi);
}else{
bos = new BufferedOutputStream(new FileOutputStream(upliadFile));
byte[] date = new byte[5*1024];
int len = bis.read(date);
while (len!=-1){
bos.write(date);
len = bis.read(date);
}
}
}
}catch(Exception e){
e.printStackTrace();
} finally {
try {
if (bis != null)
bis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
if (bos != null)
bos.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
try{
db.insertObj(sql);
return mapping.findForward("success");
}catch(Exception e){
e.getMessage() ;
}
}
return mapping.findForward("error");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -