JavaWeb Struts文件上传功能实现详解
程序员文章站
2024-03-13 08:51:51
一.struts文件上传的思路
之前也讲过了fileupload这个组件,功能很强大,但是操作繁琐复杂。这次,在strust中也对文件上传有所支持,并fileupload...
一.struts文件上传的思路
之前也讲过了fileupload这个组件,功能很强大,但是操作繁琐复杂。这次,在strust中也对文件上传有所支持,并fileupload进行了包装,用起来方便了许多。
这里涉及到了一个标签和一个类:
<html:file property="对应actionform中的属性名称">
用这个标签可以上传文件,但是要接收次内容必须依靠org.apache.struts.upload.formfile
接口完成。
二.实现
sh.jsp:
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title>sh.jsp</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> <body> <html:form action="/jsp/upload.do" method="post" enctype="multipart/form-data"> <html:file property="photo"></html:file> <html:submit value="上传"></html:submit> </html:form> </body> </html>
uploadform.java:
package com.zyy.struts.form; import org.apache.struts.action.actionform; import org.apache.struts.upload.formfile; public class uploadform extends actionform { private formfile photo; public formfile getphoto() { return photo; } public void setphoto(formfile photo) { this.photo = photo; } }
iptimestamp.java:
package com.zyy.util; import java.text.simpledateformat; import java.util.date; import java.util.random; public class iptimestamp { private simpledateformat sdf = null; private string ip = null; public iptimestamp() { } public iptimestamp(string ip) { this.ip = ip; } public string getiptimerand() { stringbuffer buf = new stringbuffer(); if (this.ip != null) { string s[] = this.ip.split("\\."); for (int i = 0; i < s.length; i++) { buf.append(this.addzero(s[i], 3)); } } buf.append(this.gettimestamp()); random r = new random(); for (int i = 0; i < 3; i++) { buf.append(r.nextint(10)); } return buf.tostring(); } public string getdate() { this.sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss.sss"); return this.sdf.format(new date()); } public string gettimestamp() { this.sdf = new simpledateformat("yyyymmddhhmmsssss"); return this.sdf.format(new date()); } private string addzero(string str, int len) { stringbuffer s = new stringbuffer(); s.append(str); while (s.length() < len) { s.insert(0, "0"); } return s.tostring(); } public static void main(string args[]) { system.out.println(new iptimestamp("192.168.1.1").getiptimerand()); } }
uploadaction.java:
package com.zyy.struts.action; import java.io.file; import java.io.fileoutputstream; import java.io.inputstream; import java.io.outputstream; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.apache.struts.action.action; import org.apache.struts.action.actionform; import org.apache.struts.action.actionforward; import org.apache.struts.action.actionmapping; import com.zyy.struts.form.uploadform; import com.zyy.util.iptimestamp; public class uploadaction extends action { public actionforward execute(actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception { uploadform uploadform = (uploadform) form; iptimestamp ips = new iptimestamp(request.getremoteaddr()); // 文件名 string filename = ips.getiptimerand() + "." + uploadform.getphoto().getfilename().split("\\.")[uploadform .getphoto().getfilename().split("\\.").length - 1]; // 输出路径 file outfile = new file(super.getservlet().getservletcontext() .getrealpath("/") + "upload" + file.separator + filename); // 存放图片的文件夹 file file = new file(super.getservlet().getservletcontext() .getrealpath("/") + "upload"); if (!file.exists()) { file.mkdir(); } inputstream input = uploadform.getphoto().getinputstream(); outputstream output = new fileoutputstream(outfile); byte data[] = new byte[1024]; int temp = 0; while ((temp = input.read(data, 0, 1024)) != -1) { output.write(data); } output.close(); input.close(); return null; } }
struts-config.xml:
<?xml version="1.0" encoding="iso-8859-1" ?> <!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.2//en" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="uploadform" type="com.zyy.struts.form.uploadform"></form-bean> </form-beans> <global-exceptions> </global-exceptions> <global-forwards> </global-forwards> <action-mappings> <action path="/jsp/upload" attribute="uploadform" input="/jsp/sh.jsp" name="uploadform" scope="request" type="com.zyy.struts.action.uploadaction"> </action> </action-mappings> <message-resources parameter="resource.messageresources" /> </struts-config>
由于我保存在的是super.getservlet().getservletcontext().getrealpath("/")+ "upload"
这是虚拟目录的真实路径的upload文件夹下。
由此可见,在struts中的文件上传原理和fileupload一样,但是struts进行了包装,所以用起来明显比单独用fileupload组件方便了许多。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。