java文件上传方法
文件上传方法(一次上传一个文件,多个文件的话,请写循环调用)
Upload.uploadFile(theFile, filePath)
说明:
theFile:类型是FormFile
filePath:action中路径获取方法 this.getServlet().getServletContext().getRealPath("/")
调用此方法返回文件上传后的路径名
上传多个文件时,请设置每个文件之间1秒的延迟,否则文件会被覆盖
package common.com;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.struts.upload.FormFile;
/**
* @author lsc
* 文件上传
*/
public class Upload {
/**
* 文件上传
* @param theFile(FormFile)
* @param filePath(action中路径获取方法:this.getServlet().getServletContext().getRealPath("/") )
* @return
*/
public static String uploadFile(FormFile theFile , String filePath) {
String fileName = theFile.getFileName();//取得上传的Msds文件名
try{
String hardPath = "";
/*
* 取当前系统路径D:\Tomcat5\webapps\coka\ 其中coka 为当前context
*/
//String filePath = this.getServlet().getServletContext().getRealPath("/");
File savePath = new File(filePath + "UploadFiles\\");
if (!savePath.exists()) {
savePath.mkdir();
}
if
相关文档:
List的用法
List包括List接口以及List接口的所有实现类。因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法,如表1所示。
表1 List接口定义的常用方法及功能
从表1可以看出,List接口提供的适合于自身的 ......
StringBuilder path = new StringBuilder(request.getScheme());
path.append("://").append(request.getServerName());
path.append(":").append(request.getServerPort()).append(request.getContextPath());
System.out.println("***********path:" + path);
Syste ......
JDK1.5中,String类新增了一个很有用的静态方法String.format():
format(Locale l, String format, Object... args) 使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
format(String format, Object... args) 使用指定的格式字符串和参数返回一个格式化字符串。
举几个这个方法实用的例子(注释是输出结果): ......
JXL.JAR 操作Excel文件开源包
这里简单讲些项目中用到的一些方法!(项目的环境是Struts2 hibernate3 resin3 )
// Excel文件存放路径
String path = ServletActionContext.getServletContext().getRealPath("")+"userfiles\\train_excel\\text.xls";
// 生成Excel文件
WritableWorkbook wwb = Workbook.creat ......
java(Web)中相对路径,绝对路径问题总结
前言:
前一段时间,由于在处理Web应用下的文件创建与移动等,因此涉及到很多关于java
中相对路径,绝对路径等问题。同时,对于Web应用中的相对路径,绝对路径,以及Java.io.File
类学习了一下。也找了一些资料。希望大家遇到类似的问题,可以更有效的解决。
========= ......