📄 uploadaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.songbing.upload.struts.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
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.action.ActionMessage;
import org.apache.struts.upload.FormFile;
import com.songbing.upload.struts.form.UploadForm;
/**
* MyEclipse Struts
* Creation date: 12-08-2008
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class UploadAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadForm uploadForm = (UploadForm)form;
FormFile ff = uploadForm.getFile();
String fileName = ff.getFileName();
int fileSize = ff.getFileSize();
// String path1=request.getServletPath();
// System.out.println("path1:"+path1);
// System.out.println(request.getSession().getServletContext().getRealPath("/"));
String systemPath = request.getSession().getServletContext().getRealPath("");
String filePath=systemPath+"/file";
System.out.println(filePath);
File file=new File(filePath,fileName);
byte a[]=new byte[fileSize];
try{
InputStream in=ff.getInputStream();
int read=0; //实际读出的文件长度
int allread=0; //共读出的文件长度
while(allread<fileSize){
read=in.read(a,allread,fileSize);
allread+=read;
}
FileOutputStream out=new FileOutputStream(file);
out.write(a);
in.close();
out.close();
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -