zoukankan      html  css  js  c++  java
  • 文件上传

    package com.born.util;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;

    /**
    * 文件处理
    * @author asus
    *
    */
    public class FileUtil {
    public static boolean copy(File src,File dest){
    BufferedInputStream bis=null;
    BufferedOutputStream bos=null;
    try {
    bis=new BufferedInputStream(new FileInputStream(src));
    bos=new BufferedOutputStream(new FileOutputStream(dest));
    byte[] bts=new byte[1024];
    int len=-1;
    while((len=bis.read(bts))!=-1){
    bos.write(bts,0,len);
    }
    return true;
    } catch (Exception e) {
    e.printStackTrace();
    return false;
    }finally{
    if(bis!=null){
    try {
    bis.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    if(bos!=null){
    try {
    bos.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }

    }

    package com.born.action;

    import java.io.File;

    import org.apache.struts2.ServletActionContext;

    import com.born.util.FileUtil;

    /**
    * 文件上传Action
    *
    * @author asus
    *
    */
    public class UploadAction {
    /**
    * 接收拦截器传入的临时文件
    */
    private File some;
    /**
    * 接收拦截器注入的原始文件名
    */
    private String someFileName;

    public String Upload() {
    if (some == null)
    return "error";
    // 将文件放于项目部署路径下的upload文件夹下
    String path = "WEB-INF/jsp/" + someFileName;
    // 根据相对部署路径计算完整路径
    path = ServletActionContext.getServletContext().getRealPath(path);
    // 将临时文件复制到上述路径下
    FileUtil.copy(some, new File(path));

    return "success";
    }

    public File getSome() {
    return some;
    }

    public void setSome(File some) {
    this.some = some;
    }

    public String getSomeFileName() {
    return someFileName;
    }

    public void setSomeFileName(String someFileName) {
    this.someFileName = someFileName;
    }

    }

    <!--上传文件示例 -->
    <package name="demo" namespace="/demo" extends="struts-default">
    <!-- 打开上传文件页面 -->
    <action name="toUpload">
    <result name="success">/WEB-INF/jsp/upload.jsp</result>
    </action>
    <!--上传文件 -->
    <action name="upload" class="com.born.action.UploadAction"
    method="Upload">
    <result name="success">/WEB-INF/jsp/ok.jsp</result>
    <result name="error">/WEB-INF/jsp/error2.jsp</result>
    </action>
    </package>

    <%@ 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>My JSP 'upload.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

    </head>

    <body>
    <!-- 上传文件对表单有2个要求
    1,method=”post“
    2,enctype="multipart/form-data"
    -->
    <form action="upload.do" method="post" enctype="multipart/form-data">
    <input type="file" name="some" /> <input type="submit" value="提交" />
    </form>
    </body>
    </html>

  • 相关阅读:
    洛谷 P3808 【模板】AC自动机(简单版) 题解
    O3优化模板
    洛谷 P3909 异或之积 题解
    洛谷 P3870 [TJOI2009]开关 题解
    洛谷 P1891 疯狂LCM 题解
    洛谷 P5221 Product 题解
    洛谷 P2568 GCD 题解
    洛谷 P5639 【CSGRound2】守序者的尊严 题解
    扩展kmp板子
    [JZOJ3167] 【GDOI2013模拟3】查税
  • 原文地址:https://www.cnblogs.com/xuehen/p/4292684.html
Copyright © 2011-2022 走看看