用JAVA程序如何在D盘根目录中建立文件夹保存上传过来的文件,以及如何计算文件夹大小
2个回答
2013-08-30
展开全部
这个是用框架做的用的Struts2需要你加框架和jsp页面的只能给你些代码自己看看了 其实也都通用的 package actions;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport{
private String username;
private File upload;
private String uploadFileName;
private String uploadContentType;
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public File getUpload() {
return upload;
} public void setUpload(File upload) {
this.upload = upload;
} public String getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} public String getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
InputStream fin=new FileInputStream(upload);
String root=ServletActionContext.getRequest().getRealPath("upload");
//root获取上传文件的服务器目录;
//String root="d:/upload";
File file=new File(root,uploadFileName);//root的位置可以换成相对的路径
OutputStream fos=new FileOutputStream(file);
byte[] buffer=new byte[1024];
int len=0;
while((len=fin.read(buffer))>0)
{
fos.write(buffer,0,len);
}
fin.close();
fos.close();
return SUCCESS;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport{
private String username;
private File upload;
private String uploadFileName;
private String uploadContentType;
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public File getUpload() {
return upload;
} public void setUpload(File upload) {
this.upload = upload;
} public String getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} public String getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
InputStream fin=new FileInputStream(upload);
String root=ServletActionContext.getRequest().getRealPath("upload");
//root获取上传文件的服务器目录;
//String root="d:/upload";
File file=new File(root,uploadFileName);//root的位置可以换成相对的路径
OutputStream fos=new FileOutputStream(file);
byte[] buffer=new byte[1024];
int len=0;
while((len=fin.read(buffer))>0)
{
fos.write(buffer,0,len);
}
fin.close();
fos.close();
return SUCCESS;
}
}
2013-08-30
展开全部
File fileweiter = new File("D:\\admin111111");//存放目的地
fileweiter.isDirectory();//判断是否是文件夹
File filereader = new File("D:\\admin111111\\1.txt"); //文件来源
filereader.isFile();//判断是否是文件
try {
fileweiter = new File("D:\\admin111111\\admin.txt");
BufferedWriter weiter = new BufferedWriter(new FileWriter(fileweiter,true));//把你把文件保存
BufferedReader reader = new BufferedReader(new FileReader(filereader));//把你要上传的文件 读进来
weiter.write("");
String rd = reader.readLine();
while(rd!=null&&rd!="")
{
weiter.write(rd);
rd = reader.readLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 整个过程就是这样的一个模式 当然我这个写的不对 只是告诉你一个工作原理只要你能获取到客户端的一个file就可以把他写入你想放大的地方 可以通过流的形式
fileweiter.isDirectory();//判断是否是文件夹
File filereader = new File("D:\\admin111111\\1.txt"); //文件来源
filereader.isFile();//判断是否是文件
try {
fileweiter = new File("D:\\admin111111\\admin.txt");
BufferedWriter weiter = new BufferedWriter(new FileWriter(fileweiter,true));//把你把文件保存
BufferedReader reader = new BufferedReader(new FileReader(filereader));//把你要上传的文件 读进来
weiter.write("");
String rd = reader.readLine();
while(rd!=null&&rd!="")
{
weiter.write(rd);
rd = reader.readLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 整个过程就是这样的一个模式 当然我这个写的不对 只是告诉你一个工作原理只要你能获取到客户端的一个file就可以把他写入你想放大的地方 可以通过流的形式
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询