Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/build/
/target/
/bin/
/uploads/
43 changes: 41 additions & 2 deletions src/com/javawebapp/model/Podcast.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.javawebapp.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;

import javax.persistence.Column;
Expand Down Expand Up @@ -87,11 +92,45 @@ public void setPath(String path)
{
this.path = path;
}

public static boolean savePodcastToFileStore(InputStream is, String path)
{
OutputStream os = null;
try
{
os = new FileOutputStream(new File(path));

int read = 0;
//TODO is this the right size for the byte array?
byte[] bytes = new byte[1024];

while ((read = is.read(bytes)) != -1)
{
os.write(bytes, 0, read);
}
}
catch (IOException e) {
e.printStackTrace(); //TODO log
return false;
}
finally
{
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}
return true;
}

public static String constructPodcastPath(String episodeName)
{
String basePath = "C:\\Users\\Evan\\workspace\\JavaWebApplication\\uploads\\";
//TODO check for uniqueness
return basePath + episodeName;
//TODO check for uniqueness and save as an appropriate file
return basePath + episodeName + ".mp3";
}
}
12 changes: 12 additions & 0 deletions src/com/javawebapp/servlet/session/FileUploadDBServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
Podcast podcast = new Podcast(episodeName, episodeDescription, episodePath);
PodcastDaoImpl podcastDao = new PodcastDaoImpl();
podcastDao.insertPodcast(podcast);
Podcast.savePodcastToFileStore(is, episodePath);
//TODO store upload in files system outside DB
message = "Success!";
}
Expand All @@ -78,6 +79,17 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}
}

if(is != null)
{
try
{
is.close();
}
catch(Exception e)
{
logger.error("error closing input stream", e);
}
}
//set message in request scope
request.setAttribute("message", message);

Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/jsps/Message.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</head>
<body>
<center>
<h3><%=request.getAttribute("Message")%></h3>
<h3><%=request.getAttribute("message")%></h3>
</center>
</body>
</html>