丄諦啲仇魜ヤ
如 果 敌 人 让 你 生 气 , 那 说 明 你 没 有 胜 他 的 把 握!
posts - 6,comments - 56,trackbacks - 1
JSP页面
 <%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
 <head>
  <title>JSP for FileForm form</title>
 </head>
 <body>
  <html:form action="/file" enctype="multipart/form-data">
   上传 : <html:file property="myFile"/><html:errors name="fileError"/>
   <html:submit value="上传"/>
  </html:form>
  <html:img src="image/dd.jpg"/>
 </body>
</html>

/*******************************************************************************/
对应的ActionForm
package wsq.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
 * MyEclipse Struts
 * Creation date: 09-17-2007
 *
 *   @author 王世清
 **/
public class FileForm extends ActionForm {
 
 private static final long serialVersionUID = 1L;

 private FormFile myFile;

 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  return null;
 }

 public void reset(ActionMapping mapping, HttpServletRequest request) {
  
 }

 
 public static long getSerialVersionUID() {
  return serialVersionUID;
 }

 public FormFile getMyFile() {
  return myFile;
 }

 public void setMyFile(FormFile myFile) {
  this.myFile = myFile;
 }

 

 
}


 

/**********************************************************/
对应的Action


package wsq.struts.action;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import wsq.struts.form.FileForm;

public class FileAction extends Action {
   

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)  throws IOException{
  FileForm fileForm = (FileForm) form;
  
  FormFile file = fileForm.getMyFile();
  if (file == null)
  {  
   return mapping.findForward("error");
  }
 String dir=servlet.getServletContext().getRealPath("/image");//首先在你的项目下新建个(在 webroot下)image文件夹
/***注释的是按孙卫琴书上来的*************/
//  InputStream in=file.getInputStream();
//  OutputStream out=new FileOutputStream(dir+"/"+file.getFileName());
//  int bytesRead=0;
//  byte [] buffer=new byte[8000];
//  while((bytesRead=in.read(buffer, 0, 8000))!=-1)
//  {
//   out.write(buffer,0,bytesRead);
//   
//  }
  
  FileOutputStream out = new FileOutputStream(dir+"/" + file.getFileName());
  out.write(file.getFileData());

  out.close();
  //in.close();
  file.destroy();
 
  System.out.println(file.getFileName()+"上传文件的名字");
  return null;
 }
}




/*************************************/
大家还要记得传图片是会出现文件名乱码 解决的办法是写个过滤器
  要是不想写可以在
               你 的TOMCATE目录下的\webapps\examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.java
TOMCAT也为你写好一个现成的  拿来就可以用了
这两天尝试 用AJAX来写一个文件上传的  呵呵。。。




posted on 2007-09-20 12:06 Crying 阅读(826) 评论(2)  编辑  收藏 所属分类: Jsp+Struts

FeedBack:
# re: struts中的文件上传
2007-10-11 16:03 | Crying
下载

public ActionForward download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("entering 'AttachmentAction.download()' method...");
}
ActionMessages messages = new ActionMessages();

String id = request.getParameter("id");
String attachmentFile=request.getParameter("file");
String type = request.getParameter("type");
if (id != null||attachmentFile!=null) {
Attachment attachment =null;
if(id!=null) {
attachment= mgr.view(id);
} else if(attachmentFile!=null) {
attachment=mgr.viewByFile(attachmentFile);
}

if (attachment == null) {
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
"object.miss"));
saveMessages(request, messages);
return mapping.findForward("failure");
}
//filename=new String(filename.getBytes("iso-8859-1"),"gb2312");
//String filepath=this.getServletContext().getRealPath("/upload");
File file = new File(mgr.getRoot()+"/"+attachment.getAttachmentFile());
String fileName = URLEncoder.encode(attachment.getAttachment(),
"UTF-8");
BufferedInputStream br = new BufferedInputStream(
new FileInputStream(file));
byte[] buf = new byte[1024 * 1024];
int len = 0;
response.reset(); //纯下载方式
//response.setContentType("application/x-msdownload");
if (type == null) {
response
.setContentType("application/octet-stream;charset=utf-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
} else if (type != null && type.equals("jpg")) {
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}
return null;
}
  回复  更多评论
  
# re: struts中的文件上传和下载
2007-12-21 18:02 | yh
谢谢上面这位了。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: