<%@ page import="com.liferay.portal.util.Constants" %>
<%@ page import="com.liferay.portal.util.PropsUtil" %>
<%@ page import="com.liferay.util.BrowserSniffer" %>
<%@ page import="com.liferay.util.ParamUtil" %>
<%
String editorImpl = "simple";
if (BrowserSniffer.is_rtf(request)) {
editorImpl = ParamUtil.get(request, "editorImpl", PropsUtil.get(EDITOR_WYSIWYG_IMPL_KEY));
}
//editorImpl = "fckeditor";
//editorImpl = "liferay";
//editorImpl = "simple";
editorImpl = "tinymce";
// Resin won't allow dynamic content inside the jsp:include tag
RequestDispatcher rd = application.getRequestDispatcher(Constants.TEXT_HTML_DIR + "/js/editor/" + editorImpl + ".jsp");
rd.include(request, response);
%>
<%--<jsp:include page='<%= Constants.TEXT_HTML_DIR + "/js/editor/" + editorImpl + ".jsp" %>' />--%>
package com.family.fileupload.web.action;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.family.fileupload.web.form.FileUploadForm;
import com.family.fileupload.web.utils.FileUpload;
public class FileUploadAction extends DispatchAction {
public ActionForward uploadImage(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
FileUploadForm uploadForm = (FileUploadForm) form;
FileUpload fu = new FileUpload(uploadForm.getFile());
String realPath = this.getRealPath("/pictures");
String fileName = this.getFileKeyRule() + "." + fu.getExtendName().toLowerCase();
String filePath = realPath + "/" + fileName;
fu.saveToFile(filePath);
request.setAttribute("fileUrl", this.getRootUrl(request) + "/pictures/" + fileName);
return mapping.findForward("image.success");
}
private String getRootUrl(HttpServletRequest request) {
StringBuffer buf = request.getRequestURL();
int length = request.getRequestURI().length();
buf.delete(buf.length() - length, buf.length());
return buf.toString();
}
private String getRealPath(String floder) {
return this.getServlet().getServletConfig().getServletContext().getRealPath(floder);
}
private String getFileKeyRule() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int iYear = cal.get(Calendar.YEAR);
int iMonth = cal.get(Calendar.MONDAY) + 1;
int iDay = cal.get(Calendar.DAY_OF_MONTH);
int iHour = cal.get(Calendar.HOUR_OF_DAY);
int iMinute = cal.get(Calendar.MINUTE);
int iSecond = cal.get(Calendar.SECOND);
StringBuffer strKey = new StringBuffer(15);
strKey.append(iYear);
if (iMonth < 10) {
strKey.insert(4, '0');
strKey.insert(5, iMonth);
} else {
strKey.insert(4, iMonth);
}
if (iDay < 10) {
strKey.insert(6, '0');
strKey.insert(7, iDay);
} else {
strKey.insert(6, iDay);
}
if (iHour < 10) {
strKey.insert(8, '0');
strKey.insert(9, iHour);
} else {
strKey.insert(8, iHour);
}
if (iMinute < 10) {
strKey.insert(10, '0');
strKey.insert(11, iMinute);
} else {
strKey.insert(10, iMinute);
}
if (iSecond < 10) {
strKey.insert(12, '0');
strKey.insert(13, iSecond);
} else {
strKey.insert(12, iSecond);
}
return strKey.toString();
}
}
package com.family.fileupload.web.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class FileUploadForm extends ActionForm {
private static final long serialVersionUID = 1L;
private FormFile file = null;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
package com.family.fileupload.web.utils;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts.upload.FormFile;
public class FileUpload {
private FormFile file;
private int fileSize;
private String fileName;
private String extendName;
private String contentType;
public FileUpload (FormFile file) {
this.file = file;
fileName = this.file.getFileName();
fileSize = this.file.getFileSize();
contentType = this.file.getContentType();
int position = this.file.getFileName().indexOf(".");
extendName = this.file.getFileName().substring(position + 1, this.file.getFileName().length());
}
public void saveToFile(String fileName) {
try {
InputStream is = this.file.getInputStream();
int bytesRead = 0;
byte[] buffer = new byte[8912];
OutputStream os = new FileOutputStream(fileName);
while ((bytesRead = is.read(buffer, 0, 8912)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getContentType() {
return contentType;
}
public String getExtendName() {
return extendName;
}
public FormFile getFile() {
return file;
}
public String getFileName() {
return fileName;
}
public int getFileSize() {
return fileSize;
}}
<form action="/c/portal/fileUpload?method=uploadImage" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
<script>
function insertImage() {
var imageUrl = '<%=(String) request.getAttribute("fileUrl")%>';
if (imageUrl != '') {
opener.document.all("src").value = imageUrl;
window.close();
}
}
</script>
*/
function fileBrowserCallBack(field_name, url, type, win) {
//打開(kāi)文件上傳窗口
win.open("upload.jsp");
}
<form-beans>
......
<form-bean name="fileUploadForm" type="com.family.fileupload.web.form.FileUploadForm" />
</form-beans>
<action-mappings>
……
<action
name="fileUploadForm"
path="/portal/fileUpload"
scope="request"
type="com.family.fileupload.web.action.FileUploadAction"
parameter="method">
<forward name="file.success" path="/filePath.jsp"/>
<forward name="image.success" path="/imagePath.jsp"/>
</action>
</action-mappings>
聯(lián)系客服