public class FileUpLoad extends ActionSupport{
//"多文件上传就用list就可以了private List<File> 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;
}
}
struts2实现文件上传 代码
最新推荐文章于 2014-06-19 13:42:13 发布