问题
I have made a application where we can upload any file which will save in our local given directory. I want to modify it, i want to add a drop down (with multiple option i.e floor, store, section) for department. i.e if we want to upload a file in 'Store' folder, we can choose 'Store' option and the file will uploaded to the 'Store' folder. Same for 'Floor' and 'Section'. I just need any example link for that. i have made it in liferay.
import org.apache.commons.io.FileUtils;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class UploadDirectory extends MVCPortlet {
private final static int ONE_GB = 1073741824;
private final static String baseDir = "/home/xxcompny/workspace";
private final static String fileInputName = "fileupload";
public void upload(ActionRequest request, ActionResponse response)
throws Exception {
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
long sizeInBytes = uploadRequest.getSize(fileInputName);
if (uploadRequest.getSize(fileInputName) == 0) {
throw new Exception("Received file is 0 bytes!");
}
File uploadedFile = uploadRequest.getFile(fileInputName);
String sourceFileName = uploadRequest.getFileName(fileInputName);
File folder = new File(baseDir);
if (folder.getUsableSpace() < ONE_GB) {
throw new Exception("Out of disk space!");
}
File filePath = new File(folder.getAbsolutePath() + File.separator + sourceFileName);
FileUtils.copyFile(uploadedFile, filePath);
}
}
JSP is here
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>
<portlet:defineObjects />
<portlet:actionURL name="upload" var="uploadFileURL"></portlet:actionURL>
<aui:form action="<%= uploadFileURL %>" enctype="multipart/form-data" method="post">
<select name="folder">
<option value="store">Store</option>
<option value="floor">Floor</option>
<option value="department">Department</option>
</select>
<aui:input type="file" name="fileupload" />
<aui:button name="Save" value="Save" type="submit" />
</aui:form>
i want the file will upload in the belonging folder.
回答1:
I had somewhat similar task to upload files to specified folders, so following is bit modified code as per your requirement:
public void upload(ActionRequest request, ActionResponse response)
throws Exception {
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
long sizeInBytes = uploadRequest.getSize(fileInputName);
if (sizeInBytes == 0) {
throw new Exception("Received file is 0 bytes!");
}
File uploadedFile = uploadRequest.getFile(fileInputName);
String sourceFileName = uploadRequest.getFileName(fileInputName);
/* selected folder from UI */
String paramFolder = uploadRequest.getParameter("folder");
byte[] bytes = FileUtil.getBytes(uploadedFile);
if (bytes != null && bytes.length > 0) {
try {
/* Create folder if doesn't exist */
File folder = new File(baseDir + File.separator + paramFolder);
if (!folder.exists()) {
folder.mkdir();
}
/* Write file to specified location */
File newFile = new File(folder.getAbsolutePath() + File.separator + sourceFileName);
FileOutputStream fileOutputStream = new FileOutputStream(newFile);
fileOutputStream.write(bytes, 0, bytes.length);
fileOutputStream.close();
newFile = null;
} catch (FileNotFoundException fnf) {
newFile = null;
/* log exception */
} catch (IOException io) {
newFile = null;
/* log exception */
}
}
}
回答2:
You can use the below code
String user_selected_option=request.getParameter("userSel")
realPath = getServletContext().getRealPath("/files");
destinationDir = new File(realPath+"/"+user_selected_option);
// save to destinationDir
来源:https://stackoverflow.com/questions/30707794/how-to-upload-file-in-relative-directory