?? uploadaction.java
字號:
package com.webapp.upload;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
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;
/**
* <p>Title: 上傳文件</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: UploadAction.java</p>
* @author 杜江
* @version 1.0
*/
public class UploadAction extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (form instanceof UploadForm) {
UploadForm theForm = (UploadForm) form;
//獲取text數據
String text = theForm.getTheText();
//獲取傳遞的參數
String queryValue = theForm.getQueryParam();
//獲得上傳的文件
FormFile file = theForm.getTheFile();
//獲取上傳文件名
String fileName= file.getFileName();
//獲取上傳文件類型
String contentType = file.getContentType();
//獲取上傳文件尺寸大小
String size = (file.getFileSize() + " bytes");
String data = null;
try {
//獲取保存文件路徑,在web.xml中配置
String path = servlet.getServletConfig().getInitParameter("uploadpath");
//獲取文件數據
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
//寫入指定的文件
OutputStream bos = new FileOutputStream(path+fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
data = path+fileName ;
//關閉流
stream.close();
}
catch (FileNotFoundException fnfe) {
return null;
}
catch (IOException ioe) {
return null;
}
//將數據保存到request以提供display.jsp文件實用
request.setAttribute("text", text);
request.setAttribute("queryValue", queryValue);
request.setAttribute("fileName", fileName);
request.setAttribute("contentType", contentType);
request.setAttribute("size", size);
request.setAttribute("data", data);
//生成臨時文件
file.destroy();
//顯示display.jsp
return mapping.findForward("display");
}
return null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -