jsp页面,带参数上传图片
<%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts 2 File Upload</title> </head> <body topmargin="2"> <FORM action="/brand/test" method="post" ENCTYPE="multipart/form-data"> <input type="hidden" name="username" value="username_value"/> <input type="hidden" name="password" value="password_value"/> <s:file name ="myFile" /> <INPUT type="submit" value="上传"> </FORM> <s:fielderror/> </body> </html>
struts.xml
<action name="test" class="brandAction" method="test"> <interceptor-ref name="fileUpload"> <param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/pjpeg,application/msword,application/vnd.ms-excel,application/x-shockwave-flash,flv-application/octet-stream,video/x-flv </param> </interceptor-ref> <interceptor-ref name="defaultStack" /> <result name="img">/products.jsp</result> <result name="input">/products.jsp</result> <result name="success">/products.jsp</result> <result name="error">/fileupload.jsp</result> </action>
action
private static final long serialVersionUID = 0x85369b8626L; private static final int BUFFER_SIZE = 16384; private File myFile; private String fileName; public void setMyFileFileName(String fileName) { this.fileName = fileName; } public void setMyFile(File myFile) { this.myFile = myFile; } public String test() throws Exception{ HttpServletRequest request = ServletActionContext.getRequest(); String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username="+username); System.out.println("password="+password); System.out.println("default-set-file"+myFile); String root = request.getRealPath("/"); System.out.println("file-"+Upload.toFile(myFile,fileName,root,"/test/")); return SUCCESS; }
upload.java
package tools; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Date; import org.apache.struts2.ServletActionContext; public class Upload { // 上传 private static final long serialVersionUID = 0x85369b8626L; private static final int BUFFER_SIZE = 16384; public synchronized static String toFile(File path,String fileName,String root,String fileType) { String imageFileName=""; String random = String.valueOf(Math.random()).substring(2); try { Help.createDir(root+fileType); imageFileName = (new StringBuilder(String.valueOf(fileType + random))).append(getExtention(fileName)) .toString(); File imageFile = new File((new StringBuilder(String .valueOf(root))).append(imageFileName) .toString()); // 判断后缀类型 int position1 = imageFileName.lastIndexOf('.'); String fileName1 = codeToString(imageFileName .substring(position1 + 1)); if (fileName1.trim().equals("jpg") || fileName1.trim().equals("gif") || fileName1.trim().equals("bmp") || fileName1.trim().equals("swf") || fileName1.trim().equals("jpeg") || fileName1.trim().equals("pjpeg") || fileName1.trim().equals("word") || fileName1.trim().equals("excel") || fileName1.trim().equals("flv")) { InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(path), BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(imageFile), BUFFER_SIZE); byte buffer[] = new byte[BUFFER_SIZE]; while (in.read(buffer) > 0) { out.write(buffer); } } finally { if (in != null) in.close(); if (out != null) out.close(); } } } catch (Exception exception) { System.err.println(exception); } return imageFileName; } public static String getExtention(String fileName) { int pos = fileName.lastIndexOf("."); return fileName.substring(pos); } public static String codeToString(String str) {// 处理中文字符串的函数 String s = str; try { byte tempB[] = s.getBytes("iso8859-1"); s = new String(tempB); return s; } catch (Exception e) { return s; } } }
如要实现多图片上传,只要改两地方就行了。
1.jsp页面
<%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts 2 File Upload</title> </head> <body topmargin="2"> <FORM action="/brand/test" method="post" ENCTYPE="multipart/form-data"> <input type="hidden" name="username" value="username_value"/> <input type="hidden" name="password" value="password_value"/> <s:file name ="myFile" /> <s:file name ="myFile1" /> <INPUT type="submit" value="上传"> </FORM> <s:fielderror/> </body> </html>
2.action
private static final long serialVersionUID = 0x85369b8626L; private static final int BUFFER_SIZE = 16384; private File myFile; private File myFile1; private String fileName; private String fileName1; public void setMyFileFileName(String fileName) { this.fileName = fileName; } public void setMyFile(File myFile) { this.myFile = myFile; } public void setMyFile1FileName(String fileName) { this.fileName1 = fileName; } public void setMyFile1(File myFile1) { this.myFile1 = myFile1; }
这样就可以获取到值了!