Tomcat+Struts2 上传文件

1.Tomcat上传文件

Tomcat配置文件: C:\apache-tomcat-9.0.6\conf\server.xml
其中:

  • docBase是指项目ROOT所在路径,上传文件保存位置
  • path是指该项目访问的路径,上传请求的链接
<Context docBase="D:/ima" path="/pages/mes" debug="0" reloadable="false"/>

2.Struts2上传文件

Struts配置文件:struts.xml

  • 所需jar自行下载

在节点下加入:

  • struts.multipart.saveDir将上传的临时文保存到D:\ima,而不是项目的WebAppRoot+/temp 下,未知有什么作用
  • struts.multipart.maxSize表示上传文件大小限制,110241024的值表示:1MB
<constant name="struts.multipart.saveDir" value="D:\ima"></constant><!-- 临时文件夹 -->
<constant name="struts.multipart.maxSize" value="246579200" /><!-- 最大上传1*1024*1024=1MB -->
  • 上传实现

jsp代码: form标签添加属性:enctype="multipart/form-data"

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String name = "";
if(request.getParameter("name").toString()!=""||request.getParameter("name").toString()!=null)
{ name = request.getParameter("name").toString();}

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>图片上传</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">
	<script type="text/javascript" src="<%=basePath%>assets/js/jquery-3.1.1.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>assets/js/layer/layer.js"></script>
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style>
		*{margin:0;padding:0;}
		html,body{width:100%;height:100%;border:0px solid red;}
		.div1{position:relative;width:80%;height:40px;margin-left:auto;margin-right:auto;border:0px solid green;}
		.inputs{position:relative;width:100%;height:40px;margin-left:auto;margin-right:auto;text-align:center;}
		.btn{position:relative;left:40%;width:20%;height:30px;text-align:center;border-radius:4px;background-color:rgba(67,110,238,0.8);}
	</style>
	<script type="text/javascript">
	
	function check(){
	var imas = document.getElementById("imas").value.length;
	if(imas > 0)
	{return true;}
	else
	{alert("请选择文件!");return false;}
	}
	</script>
  </head>
  
  <body>
  	<div class="div1">
    <s:form name="myupload" action="myupload" namespace="/pages/mes" method="post" enctype="multipart/form-data" >
    	<br><br>
    	<!-- 	<input class="inputs" type="hidden"/> -->
    	<br><br>
    	<input style="border:1px solid black;width:100%" type="file" name="image" id="imas"/>
    	<br><br>
    	<!-- <input class="inputs" type="hidden"/> -->
    	<br><br>
    	<input class="inputs" type="text" value="<%=name%>" readonly="readonly" name="name"/>
    	<br><br>
    	<input class="btn" type="submit" value="保存" onclick="return check();"/>
    </s:form>
    </div>
    
  </body>
 
</html>

  • action
    package demo.ssh.photo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
//import org.apache.commons.io.FileUtils;
//import org.apache.struts2.ServletActionContext;
//import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings(“serial”)
public class MyUpload extends ActionSupport{
private File[] image;
private String[] imageFileName;

public String[] getImageFileName() {
	return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
	this.imageFileName = imageFileName;
}

public File[] getImage() {
	return image;
}
public void setImage(File[] image) {
	this.image = image;
}

public String addUI(){
	
	return "success";
}

private String name;

public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String execute() throws Exception{
	  //tomcat根路径:C:\apache-tomcat-9.0.6\webapps\MES\images
	  //File dir=new File(ServletActionContext.getServletContext().getRealPath("/images")); 
	  //自定义保存路径
	  File dir=new File("D://images"); 
	  if(!dir.exists()){ 
	   dir.mkdir(); //新建images文件夹
	  } 
	  //System.out.println(dir);
	  //System.out.println("-------------------------------------------------------------");
	  //System.out.println(image);
	  //System.out.println(imageFileName);
	  //Date date = new Date();
	  //SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMddhhmmss");
	  for(int i = 0;i<image.length;i++)
	  {
		  String kuozhanString = imageFileName[i].substring(imageFileName[i].lastIndexOf(".")+1);
		  FileInputStream in=null; 
		  FileOutputStream out=null;
		  try{ 
		   in= new FileInputStream(image[i]);
		   out=new FileOutputStream(dir + "\\" +this.name/*+String.valueOf(i)*/+"."+kuozhanString); 
		   byte[] b=new byte[1024*1024];//
		   int j=0; 
		   while((j=in.read(b))>0){ 
		    out.write(b,0,j); 
		   } 
		   in.close();
		   out.close(); 
		  }catch(Exception e){ 
		   e.printStackTrace(); 
		  }finally{ 
		  } 
	  }
	  return SUCCESS;
}

}

看图:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪悟道

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值