通过servlet自带的Part对象上传文件
为了代码阅读的舒适性,所以封装了上传文件跟获取歌曲信息的方法。
@MultipartConfig
@WebServlet("/wyy/addSong")
public class UploadMusicServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
// 1.获得参数
Song s = getSong(req);
// 2.获得part对象,参数值为file对象的名字
Part part = req.getPart("file");
// 3.插入歌曲信息
SongService songService = new SongServiceImpl();
int i = songService.addSong(s, part);
// 4.响应到所有歌曲的页面
resp.sendRedirect("songs");
}
/**
* 封装获得歌曲对象的信息
* @param req
* @return
*/
public static Song getSong(HttpServletRequest req) {
// 获得文件名
String name = req.getParameter("name");
// 获得歌手名
String singer = req.getParameter("singer");
String album = req.getParameter("album");
String style = req.getParameter("style");
// 文件路径
String location = "G://resource//javaForRuandi//upload_02//"+singer+"//"+name+".mp3";
Timestamp uploadTime = new Timestamp(new Date().getTime());
// 封装一个歌曲对象 此时还未获得size的值,该值是上传之后获得的,故初始化为0
Song song = new Song(0,name,singer,album,style,location,0,uploadTime);
System.out.println(song);
return song;
}
}
给不同的歌手创建不同的文件夹存放
public int addSong(Song song, Part part) {
// 1.获得参数
// 2.文件上传
long size = 0;
try {
size = upload(part,song);
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
// 重新设置size
song.setSize(size);
return songDao.insertSong(song);
}
/**
* 封装拷贝文件的信息
* @param part
* @param s
* @return
* @throws IOException
*/
private long upload(Part part, Song s) throws IOException, ServletException {
// 获得封装的part对象(参数为上传文件的name属 Part part = req.getPart("file");性)
// 获得请求part头信息
String message = part.getHeader("Content-Disposition");
// System.out.println(message);
// 文件从一个目录拷贝到服务器的目录
// 构建输出目录
File file = new File("G://resource//javaForRuandi//upload_02//"+s.getSinger());
// 如果文件夹不存在,则创建一个
if (!file.exists()){
file.mkdir();
}
// 获得输入流:只获得文件的输出流,通过http协议读取输入流
InputStream in = part.getInputStream();
// 输出流
FileOutputStream fos = new FileOutputStream(s.getLocation());
// 读写操作
byte[] bs = new byte[512];
int len = 0;
while ((len=in.read(bs))!=-1){
fos.write(bs,0,len);
}
fos.close();
in.close();
long size = new File(s.getLocation()).length();
System.out.println("size:"+size);
return size;
}
form表单
<form action="/wyy/addSong" class="form-horizontal" method="post" enctype="multipart/form-data">
<div class="form-group form-group-sm">
<label class="col-sm-2 col-md-1 control-label">音乐文件</label>
<input type="file" name="file" id="file" style="display: none;"/>
<div class="col-sm-7 col-md-6">
<button type="button" id="select_file" class="btn btn-success btn-sm">
<span class="glyphicon glyphicon-cloud-upload"></span>
选择音乐文件
</button>
<span class="file_path"></span>
</div>
</div>
<div class="form-group form-group-sm">
<label class="col-sm-2 col-md-1 control-label">歌曲名称</label>
<div class="col-sm-5 col-md-6">
<input type="hidden" name="id" value="0">
<input type="text" class="form-control" id="music_name" name="name" placeholder="歌曲名称">
</div>
</div>
<div class="form-group form-group-sm">
<label class="col-sm-2 col-md-1 control-label">歌手名称</label>
<div class="col-sm-5 col-md-6">
<input type="text" class="form-control" id="artist" name="singer" placeholder="歌手名称">
</div>
</div>
<div class="form-group form-group-sm">
<label class="col-sm-2 col-md-1 control-label">专辑名称</label>
<div class="col-sm-5 col-md-6">
<input type="text" class="form-control" id="album" name="album" placeholder="专辑名称">
</div>
</div>
<div class="form-group form-group-sm">
<label class="col-sm-2 col-md-1 control-label">歌曲风格</label>
<div class="col-sm-10 col-md-11 music_style">
<span class="label label-info">流行</span>
<span class="label label-info">摇滚</span>
<span class="label label-info">爵士</span>
<span class="label label-info">DJ</span>
<span class="label label-info">轻音乐</span>
<span class="label label-info">古风</span>
<span class="label label-info">怀旧</span>
<span class="label label-info">网络</span>
<span class="label label-info">乡村</span>
<span class="label label-info">说唱</span>
</div>
</div>
<div class="form-group form-group-sm">
<label class="col-sm-2 col-md-1 control-label sr-only">歌曲风格</label>
<div class="col-sm-5 col-md-6 music_style">
<input type="text" class="form-control" id="style" name="style" placeholder="歌曲风格" />
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-md-4 col-sm-offset-4">
<button id="btn" type="submit" class="btn btn-primary btn-block">确认上传</button>
</div>
</div>
</form>
具体的项目源码会在写完之后上传。期待早日完成的一天~