网站建设资讯

NEWS

网站建设资讯

百度上传文件java代码 java file上传

JAVA 上传下载文件

Java代码实现文件上传

10年积累的成都做网站、成都网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有鲅鱼圈免费网站建设让你可以放心的选择与我们合作。

FormFile file=manform.getFile(); 

String newfileName = null;

String newpathname=null;

String fileAddre="/numUp";

try {

InputStream stream = file.getInputStream();// 把文件读入

String filePath = request.getRealPath(fileAddre);//取系统当前路径

File file1 = new File(filePath);//添加了自动创建目录的功能

((File) file1).mkdir();   

newfileName = System.currentTimeMillis()

+ file.getFileName().substring(

file.getFileName().lastIndexOf('.'));

ByteArrayOutputStream baos = new ByteArrayOutputStream();

OutputStream bos = new FileOutputStream(filePath + "/"

+ newfileName);

newpathname=filePath+"/"+newfileName;

System.out.println(newpathname);

// 建立一个上传文件的输出流

System.out.println(filePath+"/"+file.getFileName());

int bytesRead = 0;

byte[] buffer = new byte[8192];

while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {

bos.write(buffer, 0, bytesRead);// 将文件写入服务器

}

bos.close();

stream.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

java上传文件代码

public class FileUpLoad extends ActionSupport{

//"多文件上传就用list就可以了private ListFile file;"

private File file;

//上传文本的name

public File getFile() {

return file;

}

public void setFile(File file) {

this.file = file;

}

private String fileContentType;

//上传的文件类型。

public String getFileContentType() {

return fileContentType;

}

public void setFileContentType(String fileContentType) {

this.fileContentType = fileContentType;

}

//获取上传文件的名称

private String fileFileName;

public String getFileFileName() {

return fileFileName;

}

public void setFileFileName(String fileFileName) {

this.fileFileName = fileFileName;

}

public String upload() throws Exception

{

//获取文件上传路径

String root=ServletActionContext.getRequest().getRealPath("/upload");

InputStream is=new FileInputStream(file);

String.substring(fileFileName.indexOf("."));//截取上传文件的后缀。便于新定义名称。.jpg

System.out.println(name);

File descFile=new File(root,新定义的文件名称+fileFileName.indexOf("."));

OutputStream os=new FileOutputStream(descFile);

byte[] buffer=new byte[1024];

int length=0;

while(-1!=(length=(is.read(buffer))))

{

os.write(buffer, 0, length);

}

is.close();

os.close();

return SUCCESS;

}

}

java 文件上传的代码,尽量详细一点。。。

// 这是我写的一个方法,里面只需要传两个参数就OK了,在任何地方调用此方法都可以文件上传

/**

* 上传文件

* @param file待上传的文件

* @param storePath待存储的路径(该路径还包括文件名)

*/

public void uploadFormFile(FormFile file,String storePath)throws Exception{

// 开始上传

InputStream is =null;

OutputStream os =null;

try {

is = file.getInputStream();

os = new FileOutputStream(storePath);

int bytes = 0;

byte[] buffer = new byte[8192];

while ((bytes = is.read(buffer, 0, 8192)) != -1) {

os.write(buffer, 0, bytes);

}

os.close();

is.close();

} catch (Exception e) {

throw e;

}

finally{

if(os!=null){

try{

os.close();

os=null;

}catch(Exception e1){

;

}

}

if(is!=null){

try{

is.close();

is=null;

}catch(Exception e1){

;

}

}

}

}


本文名称:百度上传文件java代码 java file上传
网站网址:http://cdweb.net/article/doediee.html