package com.tcit.util;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.upload.FormFile;
/****************************
<html:form action="#" enctype="multipart/form-data" >
******************************/
/*****************************************
*
* 图片文件上传类
*
*****************************************/
public class Upload_File {
private String FilePath="/images"; // 图片存储文件 默认 /images
private String ServierPath; //服务器图片存储路径
private String FileName; //图片文件名
@SuppressWarnings("deprecation")
public void upload( FormFile file,HttpServletRequest request ) //图片文件FORM
{
try {
InputStream stream = file.getInputStream(); //把文件读入
ServierPath = request.getRealPath(FilePath); //取当前系统路径
String file_name = file.getFileName(); //获取上传图片扩展名
int s_id = file_name.lastIndexOf(".");
String ext = file_name.substring(s_id); //文件扩展名
OutputStream img_os = new FileOutputStream(ServierPath +"/"+FileName+ext);//建立一个上传文件的输出流
//System.out.println("服务器系统路径:"+ServierPath);
//System.out.println("图片路径:"+FilePath+"图片名:"+file.getFileName()+"文件类型:"+file.getContentType());/*************************/
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read(buffer, 0, 8192))!= -1) {
img_os.write(buffer, 0, bytesRead); //将文件写入服务器
}
img_os.close();
stream.close();
}catch(Exception e){System.err.print(e);}
}
public String getFilePath() { //取得图片文件路径
return FilePath;
}
public void setFilePath(String filePath) { //设置图片文件路径
FilePath = filePath;
}
public String getFileName() { //取得图片文件名
return FileName;
}
public void setFileName(String fileName) { //自定义图片文件名
FileName = fileName;
}
}
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.upload.FormFile;
/****************************
<html:form action="#" enctype="multipart/form-data" >
******************************/
/*****************************************
*
* 图片文件上传类
*
*****************************************/
public class Upload_File {
private String FilePath="/images"; // 图片存储文件 默认 /images
private String ServierPath; //服务器图片存储路径
private String FileName; //图片文件名
@SuppressWarnings("deprecation")
public void upload( FormFile file,HttpServletRequest request ) //图片文件FORM
{
try {
InputStream stream = file.getInputStream(); //把文件读入
ServierPath = request.getRealPath(FilePath); //取当前系统路径
String file_name = file.getFileName(); //获取上传图片扩展名
int s_id = file_name.lastIndexOf(".");
String ext = file_name.substring(s_id); //文件扩展名
OutputStream img_os = new FileOutputStream(ServierPath +"/"+FileName+ext);//建立一个上传文件的输出流
//System.out.println("服务器系统路径:"+ServierPath);
//System.out.println("图片路径:"+FilePath+"图片名:"+file.getFileName()+"文件类型:"+file.getContentType());/*************************/
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read(buffer, 0, 8192))!= -1) {
img_os.write(buffer, 0, bytesRead); //将文件写入服务器
}
img_os.close();
stream.close();
}catch(Exception e){System.err.print(e);}
}
public String getFilePath() { //取得图片文件路径
return FilePath;
}
public void setFilePath(String filePath) { //设置图片文件路径
FilePath = filePath;
}
public String getFileName() { //取得图片文件名
return FileName;
}
public void setFileName(String fileName) { //自定义图片文件名
FileName = fileName;
}
}
图片上传组件详解
本文介绍了一个用于处理图片上传的Java组件,该组件使用Struts框架的FormFile进行文件读取,并实现了图片从客户端到服务器的完整上传流程。文章详细解释了如何通过获取上传文件的输入流、确定服务器上的存储路径及文件名来完成文件的保存。
108

被折叠的 条评论
为什么被折叠?



