myeclipse上传文件,java代码

这是一个使用Java实现的文件上传Action类,基于Struts2框架。代码中定义了上传文件的相关属性,并实现了文件从客户端到服务器的保存操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



package lee;

import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author  yeeku.H.lee kongyeeku@163.com
 * @version  1.0
 * <br>Copyright (C), 2005-2008, yeeku.H.Lee
 * <br>This program is protected by copyright laws.
 * <br>Program Name:
 * <br>Date:
 */

public class UploadAction extends ActionSupport
{
 //封装文件标题请求参数的属性
 private String title;
 //封装上传文件域的属性
 private File upload;
 //封装上传文件类型的属性
 private String uploadContentType;
 //封装上传文件名的属性
 private String uploadFileName;

 //直接在struts.xml文件中配置的属性
    private String savePath;
 //接受struts.xml文件配置值的方法
    public void setSavePath(String value)
 {
        this.savePath = value;
    }
 //返回上传文件的保存位置
    private String getSavePath() throws Exception
 {
        return ServletActionContext.getRequest().getRealPath(savePath);
    }
 //文件标题的setter和getter方法
 public void setTitle(String title)
 {
  this.title = title;
 }
 public String getTitle()
 {
  return (this.title);
 }
 //上传文件对应文件内容的setter和getter方法
 public void setUpload(File upload)
 {
  this.upload = upload;
 }
 public File getUpload()
 {
  return (this.upload);
 }
 //上传文件的文件类型的setter和getter方法
 public void setUploadContentType(String uploadContentType)
 {
  this.uploadContentType = uploadContentType;
 }
 public String getUploadContentType()
 {
  return (this.uploadContentType);
 }
 //上传文件的文件名的setter和getter方法
 public void setUploadFileName(String uploadFileName)
 {
  this.uploadFileName = uploadFileName;
 }
 public String getUploadFileName()
 {
  return (this.uploadFileName);
 }

 @Override
    public String execute() throws Exception
 {
  //以服务器的文件保存地址和原文件名建立上传文件输出流
  FileOutputStream fos = new FileOutputStream(getSavePath()
   + "\\" + getUploadFileName());
  FileInputStream fis = new FileInputStream(getUpload());
  byte[] buffer = new byte[1024];
  int len = 0;
  while ((len = fis.read(buffer)) > 0)
  {
   fos.write(buffer , 0 , len);
  }
        return SUCCESS;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值