您的位置:首页 > 其它

上传文件详解

2016-01-28 16:47 357 查看
jsp
----------------------------------------------------------------------------------------------------------------

版本号:

安装文件:

-------------------------------------------------------------------------------------------------------------------
controller
----------------------------------------------------------------------------------------------------
@Controller
@RequestMapping("/")
public class UploadShipSoft {

@Value("#{propertiesReader['sys.shipsoft.filepath']}")
private String uploadPath;

@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
public void uploadFile(HttpServletRequest req,HttpServletResponse resp, MultipartFile file, String version){
Map map = new HashMap();
if(file != null){
//获取实际路径,“/”指代项目根目录,所以代码返回的是项目在容器中的实际发布运行的根路径
String path = req.getSession().getServletContext().getRealPath("/");
//获取文件的名字以及格式
map.put("filename", file.getOriginalFilename());
//获取文件的大小
map.put("size", file.getSize() / (1024.0 * 1024) + "M");
try {
//获得相对路径
String filePath = File.separatorChar+uploadPath+File.separatorChar+file.getOriginalFilename();
//进行文件的保存,function附下
//从InputStream中读出内存到byte[]中然后,使用FileOutputStream写入文件中.
FileUtil.SaveFileFromInputStream(file.getInputStream(), path+filePath);

BoatsVersion boatsVersion = new BoatsVersion();

boatsVersion.setCreateTime(new Date());

boatsVersion.setVersion(version);

boatsVersion.setDomain("http://58.49.94.100/njszhd");
boatsVersion.setAddress(filePath);
resp.setCharacterEncoding("utf-8");
if(boatsVersionService.save(boatsVersion)){
//redirect 会首先发一个response给浏览器, 然后浏览器收到这个response后再发一个requeset给服务器, 然后
//服务器发新的response给浏览器. 这时页面收到的request是一个新从浏览器发来的. 跳转到一个新的页面
resp.sendRedirect("load.jsp?save=succes");
}else{
resp.sendRedirect("load.jsp?save=error"); }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

-------------------------------------------------
public static void SaveFileFromInputStream(InputStream stream,String filename) throws IOException
{
//获取文件名第一个字符的位置
int index = filename.lastIndexOf(File.separatorChar);
//截取当前文件所在目录路径
String path = filename.substring(0,index + 1);
File file=new File(path);
//file.exists():测试此抽象路径名定义的文件或目录是否存在
//file.isDirectory():java中的isDirectory()是检查一个对象是否是文件夹。返回值是boolean类型的。如果是则返回true,否则返回false。
if(!file .exists() || !file.isDirectory()){
//mkdirs()创建此抽象路径指定的目录,包括所有必须但不存在的父目录。(及可以创建多级目录,无论是否存在父目录)
file.mkdirs();
}

File saveFile = new File(filename);
if(!saveFile .exists())
//如果指定的文件不存在,创建该文件
saveFile.createNewFile();
//这将创建一个文件输出流写入到由指定的File对象表示文件。
FileOutputStream fs = new FileOutputStream(filename);
byte[] buffer =new byte[1024*1024];//存储读取数据的缓冲区
int bytesum = 0;
int byteread = 0;
//stream.read(buffer):从此输入流中将 byte.length 个字节的数据读入一个 byte 数组中。在某些输入可用之前,此方法将阻塞。
此方法只执行 read(b, 0, b.length) 调用并返回结果。注意到它不 执行 in.read(b) 很重要;FilterInputStream 的某些子类依赖于实际使用的实现策略。
//读入缓冲区的字节总数,如果因为已经到达流末尾而没有更多的数据,则返回 -1
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
//buffer:存储读取数据的缓冲区
// 0:目标数组 b 中的起始偏移量
// byyrtread:读取的最大字节数
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: