Jsp文件上传

用户常常需要上传一些文件给系统,所以就涉及到了web的文件上传。首先要在from表单中 声明 method="post" enctype="multipart/form-data"两个属性,这是必不 可少的。

然后在服务器接收参数的时候就麻烦了,

// 接收用户上传的方法
	public String upload(HttpServletRequest request, HttpServletResponse response) {
		// 准备获取数据
		Map<String, String> parameter = new HashMap<String, String>();// 字段参数表,用于存储字段参数
                String songUrl = "";
		String coverUrl = "";
		String lrcUrl = "";
                String id = UUID.randomUUID().toString();
		// 准备文件上传
		String fileName = null;// 文件名
		InputStream in = null;// 文件输入流
		FileOutputStream out = null;// 文件输出流
		File filePath = null;// 文件存储路径
		// 创建磁盘工厂,调用第三方jar包commons-fileupload-1.3.3.jar
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 创建工厂核心对象
		ServletFileUpload sfu = new ServletFileUpload(factory);
		// 核心对象解析请求
		try {
			List<FileItem> parseRequest = sfu.parseRequest(request);
			// 遍历文件项:字段或文件
			for (FileItem fileItem : parseRequest) {
				if (fileItem.isFormField()) {
					// 拿到字段,封装内容到map集合
					parameter.put(fileItem.getFieldName(), fileItem.getString("utf-8"));
				} else {
					// 拿到文件
					fileName = fileItem.getName();
					in = fileItem.getInputStream();
					// 存储文件
                                        //通过文件名采用不同的方法存到本地,单一文件上传可不要if-else
					if (fileItem.getFieldName().equals("song")) {
						// 存储音频
						// 获取文件后缀名
                                                //分割文件名
						String[] str = fileName.split("\\.");//注意“.”是要用转义字符的
						// 存入数据库的地址,每个人要存的地方不一样,一般来说存能访问的地址或本地存放地址,注意一下
						songUrl = SyayicParameter.song_path + id + "." + str[str.length - 1];//这里我调用的是一个静态属性
						filePath = new File(System.getProperty("catalina.home") + "\\webapps\\mixueFile\\song");// 文件存放路径
						//检查文件是否已经存在
                                               if (!filePath.exists()) {
							filePath.mkdirs();
						}
						out = new FileOutputStream(filePath.getAbsolutePath() + "\\" + id + "." + str[str.length - 1]);
                                                 //调用第三jar包commons-io-2.5.jar
						IOUtils.copy(in, out);
					} else if (fileItem.getFieldName().equals("cover")) {
						// 存储封面
						String[] str = fileName.split("\\.");
						// 存入数据库的地址
						if (fileName.equals("")) {
							// 用户未上传封面
							coverUrl = StaticParameter.cover_path + "default.png";
						} else {
							coverUrl = StaticParameter.cover_path + id + "." + str[str.length - 1];
							// 存入本地的地址
							filePath = new File(System.getProperty("catalina.home") + "\\webapps\\mixueFile\\cover");
							if (!filePath.exists()) {
								filePath.mkdirs();
							}
							out = new FileOutputStream(
									filePath.getAbsolutePath() + "\\" + id + "." + str[str.length - 1]);
							IOUtils.copy(in, out);
						}
					} else if (fileItem.getFieldName().equals("lrc")) {
						// 储存歌词
						String[] str = fileName.split("\\.");
						// 存入数据库的地址
						if (fileName.equals("")) {
							// 用户未上传歌词
							lrcUrl = StaticParameter.lrc_path + "default.lrc";
						} else {
							lrcUrl = StaticParameter.lrc_path + id + "." + str[str.length - 1];
							// 存入本地的地址
							filePath = new File(System.getProperty("catalina.home") + "\\webapps\\mixueFile\\lrc");
							if (!filePath.exists()) {
								filePath.mkdirs();
							}
							out = new FileOutputStream(
									filePath.getAbsolutePath() + "\\" + id + "." + str[str.length - 1]);
							IOUtils.copy(in, out);
						}
					}
				}
			}
                         //其他参数存入数据库
			if (ss.addSong(id, parameter.get("name"), songUrl, coverUrl, lrcUrl, parameter.get("author"),
					new Timestamp(System.currentTimeMillis()), 0, parameter.get("upper"))) {
				return null;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值