java传文件方法_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值