📄 portalspaceuploadservlet.java
字号:
/*
* Created on 2006-7-29
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package szgrid.portal.servlet;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Hashtable;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.misc.BASE64Encoder;
import szgrid.common.exception.SzgridExceptionUtil;
import szgrid.portal.config.PortalConfigureHelper;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import com.oreilly.servlet.multipart.FilePart;
import com.oreilly.servlet.multipart.MultipartParser;
import com.oreilly.servlet.multipart.Part;
import org.apache.log4j.Logger;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class PortalSpaceUploadServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -359254006302698237L;
private final int DEFAULT_FILE_SIZE = 10 * 1024 * 1024;
static Logger logger = Logger.getLogger(PortalSpaceUploadServlet.class);
private static Hashtable<String, String> properties = new Hashtable<String, String>();
public String setProperty(String value) {
long l = System.currentTimeMillis();
String id = String.valueOf(l);
id = "PROPERTY_" + id;
properties.put(id, value);
return id;
}
public String[] setProperty(String[] value) {
String[] ids = new String[value.length];
for (int i = 0; i < value.length; i++) {
long l = System.currentTimeMillis()+i;
String id = String.valueOf(l);
id = "PROPERTY_" + id;
properties.put(id, value[i]);
ids[i] = id;
}
return ids;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filePath = "";
try {
int maxsize;
String str = PortalConfigureHelper.getMaxSize();
if (str == null || str.length() < 1)
maxsize = DEFAULT_FILE_SIZE;
else
maxsize = Integer.parseInt(str);
filePath = new String(request.getParameter("filePath").trim()
.getBytes("ISO8859-1"), "UTF-8");
filePath = URLDecoder.decode(filePath, "UTF-8");
if (filePath.indexOf("ftp://") == 0)
uploadByFtp(request, filePath, maxsize);
if (filePath.indexOf("gftp://") == 0)
uploadByGftp(request, filePath, maxsize);
else
uploadByLocal(request, filePath, maxsize);
} catch (Throwable e) {
SzgridExceptionUtil.makeFault(e, "Can not upload file to: "
+ filePath);
}
}
private void uploadByLocal(HttpServletRequest request, String filePath,
int maxsize) throws IOException {
String userName = new String(request.getParameter("userName").trim()
.getBytes("ISO8859-1"), "GBK");
String spaceAbsolutePath = PortalConfigureHelper
.getPortalSpaceAbsolutePath();
if (!spaceAbsolutePath.endsWith("/"))
spaceAbsolutePath = spaceAbsolutePath + "/";
String fullPath = spaceAbsolutePath + userName + "/" + filePath;
File file = new File(fullPath);
if (!file.exists())
file.mkdirs();
new MultipartRequest(request, fullPath, maxsize, "utf-8",
new DefaultFileRenamePolicy());
}
private void uploadByGftp(HttpServletRequest request, String filePath,
int maxsize) throws IOException {
String passport = request.getParameter("passport");
logger.info("[passport] " + passport);
if (passport.startsWith("PROPERTY_")) {
String id = passport;
passport = properties.get(id);
properties.remove(id);
}
passport = format(passport);
String subject = request.getParameter("subject");
logger.info("[subject] " + subject);
if (subject.startsWith("PROPERTY_")) {
String id = subject;
subject = properties.get(id);
properties.remove(id);
}
subject = format(subject);
filePath = filePath.substring("gftp://".length(),filePath.length());
filePath = "ftp://"+ passport+":"+subject+"@"+filePath;
MultipartParser parser = new MultipartParser(request, maxsize, true,
true, "utf-8");
Part part1;
while ((part1 = parser.readNextPart()) != null) {
if (!part1.isFile())
continue;
FilePart filePart = (FilePart) part1;
String fileName = filePart.getFileName();
if (fileName != null) {
filePart.setRenamePolicy(new DefaultFileRenamePolicy());
InputStream partInput = filePart.getInputStream();
URL url = new URL(filePath + "/" + fileName);
OutputStream out = url.openConnection().getOutputStream();
byte buf[] = new byte[8192];
int i;
while ((i = partInput.read(buf)) != -1) {
out.write(buf, 0, i);
}
out.flush();
out.close();
partInput.close();
}
}
}
private void uploadByFtp(HttpServletRequest request, String filePath,
int maxsize) throws IOException {
MultipartParser parser = new MultipartParser(request, maxsize, true,
true, "utf-8");
Part part1;
while ((part1 = parser.readNextPart()) != null) {
if (!part1.isFile())
continue;
FilePart filePart = (FilePart) part1;
String fileName = filePart.getFileName();
if (fileName != null) {
filePart.setRenamePolicy(new DefaultFileRenamePolicy());
InputStream partInput = filePart.getInputStream();
URL url = new URL(filePath + "/" + fileName);
OutputStream out = url.openConnection().getOutputStream();
byte buf[] = new byte[8192];
int i;
while ((i = partInput.read(buf)) != -1) {
out.write(buf, 0, i);
}
out.flush();
out.close();
partInput.close();
}
}
}
private String format(String oldString) {
byte[] proxyBytes = oldString.getBytes();
BASE64Encoder encoder = new BASE64Encoder();
String proxyBase64Str = encoder.encode(proxyBytes);
String separator = System.getProperty("line.separator");
String newString = proxyBase64Str.replaceAll(separator, "_");
return newString;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -