1.创建web项目
2.加入Struts所需的jar包
3.代码:
(1)loadStruts.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
</head>
<body>
<form action="/fileload/fileload.do?action=load" method="post" enctype="multipart/form-data">
用户字:<input type="text" name="name"/>
头像: <input type="file" name="photo">
<input type="submit" value="注册用户">
</form>
</body>
</html>
(2)struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="fileForm" type="com.file.form.FileForm" />
</form-beans>
<action-mappings >
<action path="/fileload"
name="fileForm"
parameter="action"
type="com.file.action.FileAction"
scope="request">
<forward name="success" path="/WEB-INF/ok.jsp" />
<forward name="error" path="/error.jsp"/>
<forward name="userList" path="/WEB-INF/jsp/userList.jsp" />
</action>
<action path="/user"
parameter="action"
type="com.file.action.UserAction"
scope="request">
<forward name="userList" path="/WEB-INF/jsp/userList.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.file.struts.ApplicationResources" />
</struts-config>
(3)FileForm.java
package com.file.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class FileForm extends ActionForm{
private FormFile photo;
private String name;
public FormFile getPhoto() {
return photo;
}
public void setPhoto(FormFile photo) {
this.photo = photo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(4)FileForm.action
public ActionForward load(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FileForm theForm=(FileForm)form;
String name=theForm.getName();
FormFile formFile=theForm.getPhoto();
String oldFileName=formFile.getFileName();
int fileSize=formFile.getFileSize();
if(fileSize<= 10*1024*1024){
InputStream is=null;
OutputStream os=null;
try{
is=formFile.getInputStream();
//得到file文件上传到tomcat后的绝对路径
String keepFilePath = this.getServlet().getServletContext().getRealPath("/load");
//防止文件名重复,为文件重新命名
String uuid=UUID.randomUUID().toString();
int beginIndex=oldFileName.lastIndexOf(".");
String newFileName=uuid+oldFileName.substring(beginIndex, oldFileName.length());
//读取文件并写到服务器
os=new FileOutputStream(keepFilePath+"\\"+newFileName);
int len=0;
byte[] bytes=new byte[1024];
while((len=is.read(bytes))>0){
os.write(bytes, 0, len);
}
UserService us=new UserService();
User user=new User();
user.setName(theForm.getName());
user.setPhoto(oldFileName+";"+newFileName);
if(us.addUser(user)){
return mapping.findForward("success");
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
is.close();
os.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
return mapping.findForward("error");
}