我使用的是Rahul Singla开发的ExtJsFileManager http://www.rahulsingla.com/blog/2011/05/extjsfilemanager-extjs-based-file-and-image-manager-plugin-for-tinymce
这里有Php和C#示例,并且有文件处理程序。我在我的项目中使用Java SpringSource,我是这些技术的新手。我试图将C#转换为Java,但我并不完全理解C#返回给客户端的内容。
这里是workind C#代码
<%@ WebHandler Language="C#" Class="BrowserHandler" %>
using System;
using System.Web;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using MyCompany;
public class BrowserHandler : IHttpHandler
{
#region Private Members
private HttpContext context;
#endregion
#region IHttpHandler Methods
public void ProcessRequest (HttpContext context)
{
this.context = context;
string op=context.Request["op"];
string path=this.context.Request["path"];
context.Response.ContentType = "text/javascript";
//These are extra parameters you can pass to the server in each request by specifying extjsfilemanager_extraparams option in init.
string param1=context.Request["param1"];
string param2=context.Request["param2"];
switch (op)
{
case "getFolders":
this.getFolders(path);
break;
case "getFiles":
this.getFiles(path);
break;
case "createFolder":
this.createFolder(path);
break;
case "uploadFiles":
this.uploadFiles(path);
break;
}
}
public bool IsReusable
{
get
{
return false;
}
}
#endregion
#region Private Methods
private void getFolders (string path)
{
path = this.validatePath(path);
List<object> l=new List<object>();
foreach (string dir in Directory.GetDirectories(path))
{
l.Add(new
{
text = Path.GetFileName(dir),
path = dir,
leaf = false,
singleClickExpand = true
});
}
this.context.Response.Write(Globals.serializer.Serialize(l));
}
private void getFiles (string path)
{
path = this.validatePath(path);
List<object> l=new List<object>();
foreach (string file in Directory.GetFiles(path))
{
l.Add(new
{
text = Path.GetFileName(file),
virtualPath = Globals.resolveUrl(Globals.resolveVirtual(file))
});
}
this.context.Response.Write(Globals.serializer.Serialize(l));
}
private void createFolder (string path)
{
path = this.validatePath(path);
string name=this.context.Request["name"];
Directory.CreateDirectory(Path.Combine(path, name));
this.context.Response.Write(Globals.serializer.Serialize(new object()));
}
private void uploadFiles (string path)
{
context.Response.ContentType = "text/html";
path = this.validatePath(path);
string successFiles="";
string errorFiles="";
for (int i=0; i < this.context.Request.Files.Count; i++)
{
HttpPostedFile file=this.context.Request.Files[i];
if (file.ContentLength > 0)
{
string fileName=file.FileName;
string extension=Path.GetExtension(fileName);
//Remove .
if (extension.Length > 0) extension = extension.Substring(1);
if (Config.allowedUploadExtensions.Contains(extension, StringComparer.InvariantCultureIgnoreCase))
{
file.SaveAs(Path.Combine(path, fileName));
}
else
{
errorFiles += string.Format("Extension for {0} is not allowed.<br />", fileName);
}
}
}
string s=Globals.serializer.Serialize(new
{
success = true,
successFiles,
errorFiles
});
byte[] b=this.context.Response.ContentEncoding.GetBytes(s);
this.context.Response.AddHeader("Content-Length", b.Length.ToString());
this.context.Response.BinaryWrite(b);
try
{
this.context.Response.Flush();
this.context.Response.End();
this.context.Response.Close();
}
catch (Exception) { }
}
private string validatePath (string path)
{
if (string.IsNullOrEmpty(path))
{
path = Config.baseUploadDirPhysical;
}
if (!path.StartsWith(Config.baseUploadDirPhysical, StringComparison.InvariantCultureIgnoreCase) || (path.IndexOf("..") != -1))
{
throw new SecurityException("Invalid path.");
}
return (path);
}
#endregion}
这是我的代码
@RequestMapping(value = "/filehandler", method = RequestMethod.POST)
public @ResponseBody byte[] filehandler(HttpServletRequest request, HttpServletResponse response) {
String op = request.getParameter("op");
String path = uploadDataFolder; // || request.getParameter("path");
String jsondata = "{root:";
String datastr = "";
File folder = new File(path);
if (op.equals( "getFolders")){
// FOLDERS
for (File fileEntry : folder.listFiles() ) {
//File fileEntry;
if (fileEntry.isDirectory()) {
datastr += "{\"text\":\"" + fileEntry.getName() +
"\",\"path\":\"" + fileEntry.getPath() +
"\",\"leaf\":\"false\"" +
",\"singleClickExpand\":\"true\"}";
}
}
}
else if (op == "getFiles"){
// FILES
for (File fileEntry : folder.listFiles()) {
//File fileEntry;
if (fileEntry.isFile()) {
datastr += "{\"text\":\"" + fileEntry.getName() +
"\",\"virtualPath\":\"" + fileEntry.getPath() + "\"";
} else {
System.out.println(fileEntry.getName());
}
}
}
else if (op == "createFolder"){
// create folder function is not supported in our project
}
else if (op == "uploadFiles"){
// upload images
request.getAttribute("Files");
}
datastr = datastr.equals("") ? "null" : "[" + datastr + "]";
jsondata += datastr + "}";
return getBytes(jsondata);
}
对于getFolders操作,它返回
这样的JSON数据{root:[{"text":"blog","path":"C:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\NetaCommerceFrameworkAdmin\META-INF\static\images\blog","leaf":"false","singleClickExpand":"true"}{"text":"photos","path":"C:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\NetaCommerceFrameworkAdmin\META-INF\static\images\photos","leaf":"false","singleClickExpand":"true"}]}
我无法弄清楚C#代码返回的内容,因此我无法用Java编写代码。它不允许我的JSON通过“getFolders”操作返回。
答案 0 :(得分:1)
C#&#39; HTTPHandler&#39;可以返回它想要的东西,因为它可能会返回一个图像(常用),或者它可以返回二进制文件数据,使客户端可以下载文件。
它们非常灵活,可以配置为“返回”。他们经常直接写入HTTPResponse对象。
为了弄清楚你的处理程序返回什么,你想要寻找任何触及HTTPResponse的东西。
e.g。 context.Response.ContentType = "text/javascript";
这是在Http响应上设置内容类型标题。
其中的一些私有方法似乎生成了列表,并将这些序列化返回到客户端,看起来就像是JSON数据。
这里的例外情况是uploadFiles()
方法似乎接受了一个帖子请求(您可以告诉它,因为它试图访问请求对象以检索上传的文件:
HttpPostedFile file=this.context.Request.Files[i];
在进行一些检查后,似乎将文件保存到服务器。
如果我是你,我会查看Java API中的自己的序列化程序选项,看看你是否可以加入它,而不是编写自己的方法来将字符串粘在一起。