Struts FormFile 기반 파일 업로드
package zizz.struts;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
/**
* Struts FormFile .
* @author chen yuzhe
*
*/
public class FileUploadForm extends ActionForm{
/**
* serialVersionUID
*/
private static final long serialVersionUID = -7794872310588861856L;
/**
*
*/
private FormFile uploadFile;
public FormFile getUploadFile() {
return uploadFile;
}
public void setUploadFile(FormFile uploadFile) {
this.uploadFile = uploadFile;
}
}
두 번 째 단 계 는 업 로드 된 파일 을 처리 하고 서버 에 저 장 된 경 로 를 처리 하 는 Action 을 만 듭 니 다. FileUploadAction 파일 의 내용 은 다음 과 같 습 니 다.
package zizz.struts;
import java.io.FileOutputStream;
import java.io.IOException;
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 org.apache.struts.upload.FormFile;
/**
* Action, FormFile .
* @author ZIZZ
*
* @Create-Time:2007-12-26 03:35:37
*/
public class FileUploadAction extends Action {
/**
*
*/
private static String UPLOAD_FILE_PATH = "c:/income/";
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
FileUploadForm uploadForm = (FileUploadForm)form;
//
FormFile uploadFile = uploadForm.getUploadFile();
//
String fileName = uploadFile.getFileName();
//
int fileSize = uploadFile.getFileSize();
System.out.println("FileName = " + fileName);
System.out.println("FileSize=" + fileSize);
boolean result = true;
try{
//
InputStream is = uploadFile.getInputStream();
//
uploadFile(fileName,is);
}catch(IOException ex){
ex.printStackTrace();
// ,
result = false;
}
if(result){
return mapping.findForward("success");
} else {
return mapping.findForward("fail");
}
}
/**
*
* @param fileName
* @param is
* @throws IOException
*/
private void uploadFile(String fileName,InputStream is) throws IOException{
OutputStream os = new FileOutputStream(UPLOAD_FILE_PATH + fileName);
//8k
byte[] buffer = new byte[1024 * 8];
//
int len;
while((len=is.read(buffer))!=-1){
//
os.write(buffer,0,len);
}
//
os.close();
//
is.close();
}
}
세 번 째 단계, 파일 업로드 jsp 파일 작성
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> </title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<html:form action="/upload" enctype="multipart/form-data">
:<html:file property="uploadFile"></html:file>
<html:submit value=" "></html:submit>
</html:form>
</body>
</html>
네 번 째 단계, struts - config. xml 파일 설정
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="fileUploadForm" type="zizz.struts.FileUploadForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/upload" name="fileUploadForm" scope="request" type="zizz.struts.FileUploadAction">
<forward name="success" path="/success.jsp" redirect="true"></forward>
<forward name="fail" path="/fail.jsp" redirect="true"></forward>
</action>
</action-mappings>
<message-resources parameter="zizz.struts.ApplicationResources" />
</struts-config>
다섯 번 째 단계, 응용 시스템 발표, 테스트 결과 검사
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 파일 압축 및 압축 풀기파일 의 간단 한 압축 과 압축 해 제 를 실현 하 였 다.주요 테스트 용 에는 급 하 게 쓸 수 있 는 부분 이 있 으 니 불편 한 점 이 있 으 면 아낌없이 가르쳐 주 십시오. 1. 중국어 문 제 를 해 결 했 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.