sturts图片上传到服务器

 sturts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="upLoadForm" type="com.sec.form.UploadForm">
    </form-bean>

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="upLoadForm"
      input="/index.jsp"
      name="upLoadForm"
      path="/upLoad"
      scope="request"
      type="com.sec.action.UpLoadAction" />

  </action-mappings>

  <message-resources parameter="com.sec.ApplicationResources" />
</struts-config>

--------------------------------------------
UploadAction.java

package com.sec.action;

import java.io.FileOutputStream;
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.action.DynaActionForm;
import org.apache.struts.upload.FormFile;

import com.sec.form.UploadForm;


public class UpLoadAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  UploadForm uForm = (UploadForm)form;
 
  FormFile file = uForm.getImg();
 
  //保存文件
  String path = getServlet().getServletContext().getRealPath("/upload");
  String filePath = path + "/" + file.getFileName();
  saveFile(file,filePath);
 
  request.setAttribute("filePath",filePath);
  return new ActionForward("/pages/result.jsp");
}
//文件上传
private String saveFile(FormFile file,String filePath){
  String saveResult = "";
  try{
   //在这里把文件名(filePath)保存到数据中
   //在以后显示图片时,只从数据库中读取文件名
   // saveFileToDB();
   InputStream is = file.getInputStream();
   OutputStream os = new FileOutputStream(filePath);
  
   int readSize = 0;
   byte buffer[] = new byte[1024];
   while((readSize = is.read(buffer, 0, 1024)) != -1){
    os.write(buffer, 0, readSize);
   }
  }catch(Exception e){
   System.out.println("保存文件异常:"+e);
  }
 

  return saveResult;
}
}
--------------------------------------------------/index.jsp
<%@ page language="java"  contentType="text/html;charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
  <head>
    <base href="<%=basePath%>">
  </head>
 
  <body>
    <form action="/sec/upLoad.do" method="post" name="upLoadForm" enctype="multipart/form-data">
     <table>
      <tr>
       <td>
        用户名:
       </td>
       <td>
        <input type="text" name="account">
       </td>
      </tr>
      <tr>
       <td>
        密码:
       </td>
       <td>
        <input type="password" name="password">
       </td>
      </tr>
      <tr>
       <td>
        上传图片:
       </td>
       <td>
        <input type="file" name="img">
       </td>
      </tr>
      <tr>
       <td>
        <input type="submit" value="提交">
       </td>
       <td>
        <input type="button" value="清空">
       </td>
      </tr>
     </table>
    </form>
  </body>
</html>
------------------------------------------------
/pages/result.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String imgFile = (String)request.getAttribute("filePath");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

 
  <body>
  成功以下上传图片<br>
    <img src="<%= imgFile %>" width="100" height="100">
  </body>
</html>
-----------------------------------------------
UploadForm.java
package com.sec.form;

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

public class UploadForm extends ActionForm {
/**
  *
  */
private static final long serialVersionUID = 1L;
private String account;
private String password;
private FormFile img;
public String getAccount() {
  return account;
}
public void setAccount(String account) {
  this.account = account;
}
public String getPassword() {
  return password;
}
public void setPassword(String password) {
  this.password = password;
}
public FormFile getImg() {
  return img;
}
public void setImg(FormFile img) {
  this.img = img;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值