`
isiqi
  • 浏览: 16031272 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

上传图片问题

阅读更多

index.jsp
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<html>
<head>
<title>上传文件测试</title>
</head>
<body>
<form name="UploadForm" action="upload.do" method="post" enctype="multipart/form-data">
输入你的名字:<br/>
<input type="text" name="username" size="15" value="张月"/><br />
密码:<input type="password" name="userpassword" />
图形:<br />
<input type="file" name="file"><br /><br />
<input type="submit" name="Submit" value="Submit your files"/>
</form>
</body>
</html>

UploadForm.java

package forms;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class UploadForm extends ActionForm {

private java.lang.String username;
private java.lang.String userpassword;
private FormFile file;

public FormFile getFile() {
return file;
}

public void setFile(FormFile file) {
this.file = file;
}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// Validate an attribute named "xxx"
//if( getXXX() == null || getXXX().length() == 0 )
//errors.add("xxx",new ActionMessage("errors.required","xxx"));
return errors;
}

public void reset(ActionMapping mapping, HttpServletRequest request) {

super.reset(mapping, request);
}


public java.lang.String getUsername(){
return username;
}

public void setUsername(java.lang.String username) {
this.username = username;
}

public java.lang.String getUserpassword(){
return userpassword;
}

public void setUserpassword(java.lang.String userpassword) {
this.userpassword = userpassword;
}

}


UploadAction.java
package actions;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.ServletException;
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 org.apache.struts.upload.FormFile;

import forms.UploadForm;

public class UploadAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

String target = "success";
String failTarget = "fail";
// 检查输入请求是否为multipart的表单数据。
UploadForm uploadForm =(UploadForm)form;
FormFile file = uploadForm.getFile();
if(file!=null) {
String filename = file.getFileName();
System.out.println(filename+"已经上传");

//将文件保存到磁盘上
File dirFile = new File("d:/temp/images");
if(!dirFile.exists()) {
dirFile.mkdirs();
}
FileOutputStream fos = new FileOutputStream(dirFile+"/"+filename);
fos.write(file.getFileData());
fos.flush();
fos.close();

request.setAttribute("filename", filename);
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics