上传:
jsp部分内容如下(很简单,input的type=“file”就行了)
enctype="multipart/form-data" 注意加上;
<div class="contentbox">
<form id="sampleForm" name="sampleForm" action="sample/sampleSave.action"
enctype="multipart/form-data"
method="post">
<div>
<p>
<label>
<strong>样本文件:</strong>
</label>
<input id="file" type="file" name="file" class="inputbox"/>
</p>
<p>
<label>
<strong>MD5: </strong>
</label>
<input id="md5" type="text" name="md5" class="inputbox" value=""/>
</p>
<p>
<label>
<strong>MAC: </strong>
</label>
<input id="mac" type="text" name="mac" class="inputbox" value=""/>
</p>
<p>
<label>
<strong>类型: </strong>
</label>
<select id="type" name="type" >
<option value="0">--请选择--</option>
<option value="sample">样本文件</option>
<option value="dump">XXX文件</option>
<option value="alarm">xxx文件</option>
<option value="heartbeat">xxx文件</option>
</select>
</p>
<p>
<input style="margin-left:200px;" id="saveBtn" class="submit" type="button" value="提交"/>
</p>
<div id="errorMessage" class="status error" style="width:300px;display: none;">
<img src="img/icons/icon_error.png" alt="Error"/>
<span class="red" id="sampleInputError"></span>
</div>
</div>
</form>
</div>
Action部分代码:
private static final long serialVersionUID = 1L;
private SampleService sampleService;
private InputStream inputStream;
private String filename;
private String inputPath;
private static final int BUFFER_SIZE = 16 * 1024;
private List<File> file;
private List<String> fileContentType;
private List<String> fileFileName;
private String uploadPath;
//以上变量的get set方法
/**
* 页面录入,保存样本文件
*
* @return
*/
public String sampleSave() {
String path = sRequest.getSession().getServletContext().getRealPath("/");
System.out.println(path);
String md5 = sRequest.getParameter("md5");
String mac = sRequest.getParameter("mac");
String type = sRequest.getParameter("type");
if (StringUtils.isEmpty(md5) || StringUtils.isEmpty(mac) || StringUtils.isEmpty(type)) {
return ERROR;
} else {
for (int i = 0; i < fileFileName.size(); i++) {
try {
String filename = fileFileName.get(i);
String savePath = StringUtils.isEmpty(UrlAddress.getUrl(type)) ? path + "WEB-INF/uploadPath/" + type : UrlAddress.getUrl(type);
if (!new File(savePath).exists()) {
new File(savePath).mkdirs();
}
filename = savePath + "/" + filename;
// sampleName = fileFileName.get(i);
FileOutputStream fos = new FileOutputStream(filename);
InputStream is = new FileInputStream(file.get(i));
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
filename = filename.replace('\\', '/');
String ip = sRequest.getRemoteAddr();
if(sampleService.sampleSave(type, md5, mac, filename, ip)){
return SUCCESS;
}else {
return ERROR;
}
// logService.addLog(new Log(((User) session.get("user")).getId(),GlobalVar.FILEUPLOAD_ACTION,ip));
} catch (Exception e) {
e.printStackTrace();
}
}
}
return ERROR;
}
struts的配置:
<!-- 页面录入,保存文件 -->
<action name="sampleSave" class="sampleAction" method="sampleSave">
<result name="success" type="stream">
<param name="contentType">application/octet-stream; charset=UTF-8</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment; filename="${filename}"</param>
<param name="bufferSize">2048</param>
</result>
<result name="success" type="redirect">sampleQry.action</result>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</action>
上传:
/**
* 保存导入数据
*
* @return
*/
public String toolImportSave() {
// try {
// String importType = sRequest.getParameter("importType");
// System.out.println("type = "+importType);
// if("1".equals(importType.trim())){
// String realpath = ServletActionContext.getServletContext().getRealPath("/impfiles"); //D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
// System.out.println("realpath: "+realpath);
String realpath = ServletActionContext.getServletContext().getRealPath("/toolkit");
String type = sRequest.getParameter("tooltype");
if (file != null) {
System.out.println(file.length());
String behName = fileFileName.substring(fileFileName.lastIndexOf("."));
String temp = String.valueOf((new Date()).getTime());
fileFileName = temp + behName;
File savefile = new File(new File(realpath), fileFileName);
if (!savefile.getParentFile().exists()){
savefile.getParentFile().mkdirs();
}
try {
FileUtils.copyFile(file, savefile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name = sRequest.getParameter("name");
String tooltype = sRequest.getParameter("tooltype");
String toolos = sRequest.getParameter("toolos");
// String riskgrade = sRequest.getParameter("riskgrade");
String description = sRequest.getParameter("description");
String version = sRequest.getParameter("version");
// String pename = sRequest.getParameter("toolpename");
String pename = fileFileName;
Tools tool = new Tools();
tool.setDescription(description);
tool.setToolName(name);
// tool.setRiskGrade(riskgrade);
tool.setToolOS(toolos);
tool.setToolType(Integer.parseInt(tooltype));
tool.setVersion(version);
tool.setPename(pename);
peneAssistService.addTools(tool);
// }else{
// return ERROR;
// }
return SUCCESS;
// } catch (IOException e) {
// e.printStackTrace();
// return ERROR;
// }
}
jsp:
<dt><label><strong class="green">选择文件:</strong></label></dt>
<dd>
<input type="file" name="file" id="file"/>
<div class="status error" style="display:none;" id="fileError">
<img src="img/icons/icon_error.png" alt="Error" /><span class="red" id="fileErrorContext"></span>
</div>
</dd>
<dt><label><strong class="green">描述信息:</strong></label></dt>
struts:
与前个示例一样;
下载功能:
action:
/**
*
*/
private static final long serialVersionUID = 1L;
private PenetrationAssistService peneAssistService;
private File file; //上传的文件
private String fileFileName; //文件名称
private String fileContentType; //文件类型
private InputStream inputStream;
private String filename;
//get set 方法省略
public String downloadTool(){
try {
String pename = sRequest.getParameter("pename");
String typename = sRequest.getParameter("typename");
System.out.println(pename);
System.out.println(typename);
String realpath = ServletActionContext.getServletContext()
.getRealPath("/toolkit");
String enTypeName = GlobalVar.TOOLTYPES.get(typename);
enTypeName +="/"+pename;
this.inputStream = new FileInputStream(new File(realpath, enTypeName));
this.filename = pename.substring(pename.lastIndexOf("/")+1);
return SUCCESS;
} catch (FileNotFoundException e) {
e.printStackTrace();
sRequest.setAttribute("message", "文件不存在,请联系管理员!");
return ERROR;
}
}
jsp:
function download(typename,name){
$('#pename').val(name);
$('#typename').val(typename);
document.getElementById('roleForm2').submit();
}
<form action="xxxassist/toolslist.action" id="roleForm" name="roleForm" method="post">
<td><a href="javascript:download('<s:property value='#tool.typename'/>','<s:property value='#tool.pename'/>')"> </a></td>
。。。。。
struts:
<!-- 下载 -->
<action name="downloadTool" class="peneAssistAction" method="downloadTool">
<result name="success" type="stream">
<param name="contentType">application/octet-stream; charset=UTF-8</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment; filename="${filename}"</param>
<param name="bufferSize">2048</param>
</result>
</action>