Java文件上传(在SpringMVC模式下,从本地上传到服务器)
2013-12-19 10:43
381 查看
<html>页面部分重要代码
<form method="post" enctype="multipart/form-data" action="toUpLoadFile">
请选择库房文件:<input type="file" name="excelFile">
<input type="submit" value="导入" onclick="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();
}
}
页面展示:
<form method="post" enctype="multipart/form-data" action="toUpLoadFile">
请选择库房文件:<input type="file" name="excelFile">
<input type="submit" value="导入" onclick="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();
}
}
页面展示:
相关文章推荐
- ionic3 java服务器(SpringMVC) 文件的上传与下载
- Java Client/Server 上传文件到服务器与保存文件到本地
- Java SpringMVC浏览器端上传文件至服务器
- Java企业微信开发_06_素材管理之上传本地临时素材文件至微信服务器
- Java利用ssh协议实现本地文件到远程Linux服务器的上传。
- java实现多文件上传至本地服务器功能
- java调用svnkit工具类上传本地文件到svn服务器
- java实现多文件上传至本地服务器
- Java FTP 实现跨服务器文件上传下载
- java ,springMVC处理视频文件上传和下载
- java web中监听本地磁盘文件,并进行上传,复制,处理等
- 将本地文件上传到服务器上
- java FTP方式上传文件到图片服务器
- java实现Starteam检索本地未上传到服务器的目录
- java上传文件到FTP服务器
- java上传附件到本地服务器,调用linux命令然后再传输到远程服务器
- 使用 Flex 和Java servlets 将文件上传到 RED5 服务器的步骤
- SpringMVC整合fastdfs-client-java实现web文件上传下载
- java解压zip文件并上传到服务器