单个文件上传

Posted 杨荣林

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单个文件上传相关的知识,希望对你有一定的参考价值。

文件上传

//准备文件

1.上传单个文件页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>文件上传</title>
  </head>
  <body>
  <!-- 上传页面的准备 -->
  <s:form action="upload.action" enctype="multipart/form-data" method="post">
      <s:textfield name ="title" label ="标题"/><br/>
      <s:file name ="upload" label="选择文件"/><br/>
      <s:submit name ="submit" value ="上传文件"/>
  </s:form> 
  </body>
</html>

2.在struts.xml中配置相应的action

 <action name ="upload" class="action.UploadAction">
        <!--通过param参数设置保存目录的路径 -->
        <param name="savePath">/image</param>
        <result name="success">/upload_success.jsp</result>
    </action>

3.在根据action节点找对应的类

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{
    //封装上传文件的属性
    private File upload;
    //封装上传文件的类型
    private String uploadContentType;
    //封装上传文件的名称
    private String uploadFileName;
    
    public File getUpload() {
        return upload;
    }
    public void setUpload(File upload) {
        this.upload = upload;
    }
    public String getUploadContentType() {
        return uploadContentType;
    }
    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }
    public String getUploadFileName() {
        return uploadFileName;
    }
    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
    //获取文件上传的路径
    private String savePath;
    @Override
    public String execute() throws Exception {
            //创建缓存数组
            byte [] buffer =new byte[1024];
            //读取文件
                FileInputStream fis =new FileInputStream(getUpload());
                //保存文件,并设置保存目录的路径
                FileOutputStream fos =new FileOutputStream(getSavePath()+"\\\\"+this.getUploadFileName());
                int length =fis.read(buffer);
                while(length>0){
                    //每次写入length长度的内容
                    fos.write(buffer,0,length);
                    length=fis.read(buffer);
                }
                fis.close();
                fos.flush();
                fos.close();
        return SUCCESS;
    }
    public String getSavePath(){
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }
}

4.如果成功就去找成功页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>文件上传</title>
  </head>
  <body>
  <!--成功页面  -->
 上传文件成功!
 您上传的文件是:<s:property value="uploadFileName"/><br/>
 文件类型:<s:property value="uploadContentType"/>
  </body>
</html>

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

以上是关于单个文件上传的主要内容,如果未能解决你的问题,请参考以下文章

树莓派 python 如何将本地文件上传到指定的服务器页面上

SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法

在 Android 中使用具有多个布局的单个片段

Alamofire 文件上传出现错误“JSON 文本未以数组或对象开头,并且允许未设置片段的选项”

单个文件上传

vue.js 2 - 将单个文件组件的 html 片段提取到独立文件中