文件上传(Struts框架)

1.创建web项目

2.加入Struts所需的jar包

3.代码:

(1)loadStruts.jsp

       <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
     <title>文件上传</title>
  </head>
 
  <body>
       <form action="/fileload/fileload.do?action=load" method="post" enctype="multipart/form-data">
                          用户字:<input type="text" name="name"/>
                           头像:    <input type="file" name="photo">
        <input type="submit" value="注册用户">
       </form>
  </body>
</html>

(2)struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
 <data-sources />
  <form-beans >
    <form-bean name="fileForm" type="com.file.form.FileForm" />
  </form-beans>
  <action-mappings >
    <action  path="/fileload"  
      name="fileForm" 
      parameter="action"
      type="com.file.action.FileAction"
      scope="request">
     
     <forward name="success" path="/WEB-INF/ok.jsp" />
     <forward name="error" path="/error.jsp"/>
      <forward name="userList" path="/WEB-INF/jsp/userList.jsp" />
    </action>
   
    <action path="/user"
      parameter="action"
      type="com.file.action.UserAction"
      scope="request">
      <forward name="userList" path="/WEB-INF/jsp/userList.jsp" />
    </action>
   
  </action-mappings>
 
   <message-resources parameter="com.file.struts.ApplicationResources" />
</struts-config>

(3)FileForm.java

package com.file.form;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class FileForm extends ActionForm{
   private FormFile photo;
   private String name;
public FormFile getPhoto() {
 return photo;
}
public void setPhoto(FormFile photo) {
 this.photo = photo;
}
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
  
}
(4)FileForm.action

 public ActionForward load(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  
  FileForm theForm=(FileForm)form;
  String name=theForm.getName();
  FormFile formFile=theForm.getPhoto();
  String oldFileName=formFile.getFileName();
        int fileSize=formFile.getFileSize();
        if(fileSize<= 10*1024*1024){
          InputStream is=null;
             OutputStream os=null;
             try{
                 is=formFile.getInputStream();
                 //得到file文件上传到tomcat后的绝对路径
                 String keepFilePath = this.getServlet().getServletContext().getRealPath("/load");
                 //防止文件名重复,为文件重新命名
                 String uuid=UUID.randomUUID().toString();
                 int beginIndex=oldFileName.lastIndexOf(".");
                 String newFileName=uuid+oldFileName.substring(beginIndex, oldFileName.length());
                //读取文件并写到服务器
                 os=new FileOutputStream(keepFilePath+"\\"+newFileName);
                 int len=0;
                 byte[] bytes=new byte[1024];
                 while((len=is.read(bytes))>0){
                   os.write(bytes, 0, len);
                 }
                 UserService us=new UserService();
                 User user=new User();
                 user.setName(theForm.getName());
                 user.setPhoto(oldFileName+";"+newFileName);
                 if(us.addUser(user)){
                  return mapping.findForward("success");
                 }
             }catch(Exception e){
                 e.printStackTrace();
             }finally{
                 try{
                     is.close();
                   os.close();
                 }catch(IOException e){
                  e.printStackTrace();
                 }
            }
        }
      return mapping.findForward("error");
      
 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值