上传文件HTML前台

<form name="form1" encType="multipart/form-data" method="post">
<!-- <input type="file" id="inputfile" name="upload"
accept="text/plain" > -->
<input id="inputfile" type="file" style="display:none" name="file" accept=".xls,.doc,.txt,.pdf,xlsx" >
<div style="float: left; padding-left: 15px" >
<input id="photoCover" class="form-control" type="text" style="height:34px;">
</div>
<div style="float: left; padding-left: 5px" >
<a class="btn btn-info" onclick="$('input[id=inputfile]').click();">导入单个员工信息</a>
</div>
<div style="float: left; padding-left: 5px" >
<button id="uploadBtn" class="btn btn-info">导入提交</button>
</div>
</form>

$('input[id=inputfile]').change(function() {
$('#photoCover').val($(this).val());
});

新建Action 

public class UploadAction extends ActionSupport{

private File image; //上传的文件
private String imageFileName; //文件名称
private String imageContentType; //文件类型

get........set..........


public String execute() throws Exception {
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
//D:apache-tomcat-6.0.18webappsstruts2_uploadimages
System.out.println("realpath: "+realpath);
if (image != null) {
File savefile = new File(new File(realpath), imageFileName);
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("message", "文件上传成功");
}
return "success";
}

原文地址:https://www.cnblogs.com/aiwoqu/p/4146268.html