jsp页面:
<form action="uploadImg" method="post" name="myform" enctype="multipart/form-data">
<p> 用户名:<input type="text" name="user.username" /></p>
<p> 密码:<input type="password" name="user.password" /></p>
<p> 头像: <input type="file" name="uploadImage" /></p>
<input type="submit" value="提交">
</form>
Atcion页面(com.action包下的uploadImgAction.Java):
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
publicclass uploadImgAction extends ActionSupport{
private File uploadImage;//用来封装上传的文件,uploadImage为input框中name的值;
private String uploadImageContentType;//用来封装上传文件的类型,名称必须为input框中name的值+ContentType
private String uploadImageFileName;//用来封装上传文件的文件名,名称必须为input框中name的值+FileName
private String newFileName;//重写文件名,解决文件同名的问题
private String username;
private String password;
String result="error";
String sql="insert into person(username,password,img) values(?,?,?)";
String driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Sweet1";
String username="sa";
String password="123456";
Class.forName(driverClass);
Connection conn=DriverManager.getConnection(url, username, password);
PreparedStatement pstmt=conn.prepareStatement(sql);
public String execute() throws Exception{
FileInputStream fis=null;
FileOutputStream fos=null;
//定义保存的路径
String savepath=ServletActionContext.getServletContext().getRealPath("/upload");
System.out.println(savepath);
//根据路径创建文件路径对象
File file=new File(savepath);
if(!file.exists()){
file.mkdirs();
}
try {
//创建输入流
fis=new FileInputStream(uploadImage);
System.out.println(uploadImageFileName);
//创建输出流
newFileName=UUID.randomUUID() + "_" + uploadImageFileName;
System.out.println(newFileName);
fos=new FileOutputStream(savepath+"//"+newFileName);
byte buf[]=newbyte[1024];
int n=0;
while((n=fis.read(buf))!=-1){
fos.write(buf, 0, n);
}
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
pstmt.setString(1,username);
pstmt.setString(2,password);
pstmt.setString(3,newFileName);
pstmt.executeUpdate();
result="success";
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
DatabaseBean.closeStmt(pstmt);
DatabaseBean.closeConn(conn);
}
return result;
}
public File getUploadImage() {
return uploadImage;
}
publicvoid setUploadImage(File uploadImage) {
this.uploadImage = uploadImage;
}
public String getUploadImageContentType(){
return uploadImageContentType;
}
publicvoid setUploadImageContentType(StringuploadImageContentType) {
this.uploadImageContentType =uploadImageContentType;
}
public String getUploadImageFileName() {
return uploadImageFileName;
}
publicvoid setUploadImageFileName(String uploadImageFileName) {
this.uploadImageFileName =uploadImageFileName;
}
public String getNewFileName() {
return newFileName;
}
publicvoid setNewFileName(String newFileName) {
this.newFileName = newFileName;
}
publicvoid setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
publicvoid setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}
Struts.xml配置:
<struts>
<!-- 设置字符编码 -->
<constant name="struts.i18n.encoding"value="utf-8"/>
<!-- 设置自动装载国际化 -->
<constant name="struts.i18n.reload"value="true"/>
<!--设置上传文件大小 -->
<constant name="struts.multipart.maxSize"value="10240000"></constant>
<constant name="struts.multipart.saveDir" value="D://temp" ></constant>
<constant name="struts.devMode"value="true"/>
<package name="user" extends="struts-default">
<action name="uploadImg"class="com.action.uploadImgAction">
<result name="success">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>