Java文件上传(在SpringMVC模式下,从本地上传到服务器)

<html>页面部分重要代码

<form method="post" enctype="multipart/form-data"  action="toUpLoadFile">
请选择库房文件:<input type="file"  name="excelFile">
     <input type="submit" value="导入" οnclick="return impExcel();"/>
</form>


Java类

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;


import com.opensymphony.xwork2.ActionContext;


@Controller
public class UpLoadFile {


private MultipartFile  excelFile;// 得到上传的文件
private String excelFileContentType;// 得到文件的类型
private String excelFileFileName;// 得到文件的名称

public MultipartFile getExcelFile() {
return excelFile;
}


public void setExcelFile(MultipartFile excelFile) {
this.excelFile = excelFile;
}


public String getExcelFileContentType() {
return excelFileContentType;
}


public void setExcelFileContentType(String excelFileContentType) {
this.excelFileContentType = excelFileContentType;
}


public String getExcelFileFileName() {
return excelFileFileName;
}


public void setExcelFileFileName(String excelFileFileName) {
this.excelFileFileName = excelFileFileName;
}


@RequestMapping("/toUpLoadFile")
public String execute(MultipartFile excelFile,HttpServletRequest req) throws Exception {//使用MultipartFile 在SpringMVC下  不能直接用File


if (excelFile != null) {
System.out.println(excelFile.getName()+"--"+excelFile.getSize());
String filename=excelFile.getOriginalFilename(); 
SaveFileFromInputStream(excelFile.getInputStream(),req.getRealPath("resources/importExcel"),filename);//保存到服务器的路径

List<StoreEntity> entitys = readFileInfo(req.getRealPath("resources/importExcel")+"/"+filename);
ActionContext.getContext().put("message", "上传成功");


}
return "success";
}
private List<StoreEntity> readFileInfo(String string) {
// TODO Auto-generated method stub
return null;
}

//将MultipartFile 转换为File
public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException
   {      
       FileOutputStream fs=new FileOutputStream( path + "/"+ savefile);
       System.out.println("------------"+path + "/"+ savefile);
       byte[] buffer =new byte[1024*1024];
       int bytesum = 0;
       int byteread = 0; 
       while ((byteread=stream.read(buffer))!=-1)
       {
          bytesum+=byteread;
          fs.write(buffer,0,byteread);
          fs.flush();
       } 
       fs.close();
       stream.close();      
   }       
}

页面展示:




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值