第1个回答 推荐于2016-11-03
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class TestAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
xxForm cf = (xxForm)form; //相应的form类
if(cf.getConFileUP()==null){ System.out.println("无上传文件" ); }
else{
FormFile upfile = cf.getConFileUP();//在对应ActionForm中,对应字段应保存为FormFile类型
int filesize = upfile.getFileSize();
String fileName = upfile.getFileName();
System.out.println("接受到文件名称=======" + fileName );
InputStream stream = upfile.getInputStream();//把文件读入
String filePath = "D:/Tomcat 5.5/webapps/upload"+UrlUtil.getContract(); //你的上传服务器目的路径
String finalFileName = String.valueOf(System.currentTimeMillis()) + "." +
fileName.substring(fileName.indexOf(".")+1);
OutputStream bos = new FileOutputStream(filePath+"/" + finalFileName);//建立一个上传文件
int bytesRead = 0;
byte[] buffer = new byte[1024 * 1024 * 5];
while ( (bytesRead = stream.read(buffer, 0, 1024 * 1024 * 5)) != -1) {
bos.write(buffer, 0, bytesRead);//将文件写入服务器
}
bos.close();
stream.close();
}
}
}
你可以作为参考本回答被提问者采纳