本文整理汇总了Java中javax.servlet.http.Part.write方法的典型用法代码示例。如果您正苦于以下问题:Java Part.write方法的具体用法?Java Part.write怎么用?Java Part.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.http.Part
的用法示例。
在下文中一共展示了Part.write方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processRequest
import javax.servlet.http.Part; //导入方法依赖的package包/类
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>File Upload Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>File Upload Servlet</h1>");
out.println("Receiving the uploaded file ...<br>");
out.println("Received " + request.getParts().size() + " parts ...<br>");
String fileName = "";
for (Part part : request.getParts()) {
fileName = part.getSubmittedFileName();
out.println("... writing " + fileName + " part<br>");
part.write(fileName);
out.println("... written<br>");
}
out.println("... uploaded to: /tmp/" + fileName);
out.println("</body>");
out.println("</html>");
}
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:26,代码来源:FileUploadServlet.java
示例2: doPost
import javax.servlet.http.Part; //导入方法依赖的package包/类
/**
* Gestiona la subida de archivos
*/
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//obtiene el path relativo de subida de archivos de la aplicación
String savePath = getServletContext().getInitParameter("file-upload");
// crea la carpeta donde se guardaran los archivos si no existe
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
//Obtiene el archivo desde la petición
Part file = request.getPart("documento");
String fileName = getFilename(file);
//extrae la extension y el nombre de archivo por separado
int dot = fileName.lastIndexOf(".");
String fileNameExt = fileName.substring(dot);
fileName = fileName.substring(0, dot);
//remplaza cualquier caracter del tipo espacio, puntos y otros por guión bajo.
fileName = fileName.replaceAll("\\W","_");
Date fecha = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
String fechaStr = (String)sdf.format((fecha));
file.write(savePath + File.separator + fechaStr +"_"+ fileName + fileNameExt);
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
if(fileName != null) response.getWriter().write(fechaStr +"_"+ fileName + fileNameExt);
else
response.getWriter().write("");
}
示例3: doPost
import javax.servlet.http.Part; //导入方法依赖的package包/类
/**
* Gestiona la subida de archivos
*/
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//obtiene el path relativo de subida de archivos de la aplicación
String savePath = getServletContext().getInitParameter("file-upload");
// crea la carpeta donde se guardaran los archivos si no existe
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
//Obtiene el archivo desde la petición
Part file = request.getPart("documentoEvidencia");
String fileName = getFilename(file);
//extrae la extension y el nombre de archivo por separado
int dot = fileName.lastIndexOf(".");
String fileNameExt = fileName.substring(dot);
fileName = fileName.substring(0, dot);
//remplaza cualquier caracter del tipo espacio, puntos y otros por guión bajo.
fileName = fileName.replaceAll("\\W","_");
Date fecha = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
String fechaStr = (String)sdf.format((fecha));
file.write(savePath + File.separator + fechaStr +"_"+ fileName + fileNameExt);
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
if(fileName != null) response.getWriter().write(savePath + File.separator + fechaStr +"_"+ fileName + fileNameExt);
else
response.getWriter().write("");
}
示例4: doPost
import javax.servlet.http.Part; //导入方法依赖的package包/类
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// InputStream in = req.getInputStream();
PrintWriter out = resp.getWriter();
String operationStr = null;
req.setCharacterEncoding("UTF-8");
Part part = req.getPart("file");
OperationBean operationBean = new OperationBean();
if (part != null) {
String fileName = part.getSubmittedFileName();
if (fileName.length() > 0) {
part.write(fileName);
File file = new File(System.getProperty("user.dir") + JettyConfig.UPLOAD_TMP_PATH);
if (file.exists()) {
logger.info("上传成功");
operationBean.setCode(200);
operationBean.setMsg(fileName);
} else {
logger.info("上传失败");
operationBean.setCode(400);
operationBean.setMsg("");
}
}else {
SystemWSSender.error("请先浏览到您要安装的app安装包。");
}
}
operationStr = JsonUtil.beanToJson(operationBean);
logger.info(operationStr);
out.println(operationStr);
out.flush();
out.close();
}
示例5: upload
import javax.servlet.http.Part; //导入方法依赖的package包/类
protected String upload(HttpServletRequest request, StringManager smClient) {
String message = "";
try {
while (true) {
Part warPart = request.getPart("deployWar");
if (warPart == null) {
message = smClient.getString(
"htmlManagerServlet.deployUploadNoFile");
break;
}
String filename =
extractFilename(warPart.getHeader("Content-Disposition"));
if (!filename.toLowerCase(Locale.ENGLISH).endsWith(".war")) {
message = smClient.getString(
"htmlManagerServlet.deployUploadNotWar", filename);
break;
}
// Get the filename if uploaded name includes a path
if (filename.lastIndexOf('\\') >= 0) {
filename =
filename.substring(filename.lastIndexOf('\\') + 1);
}
if (filename.lastIndexOf('/') >= 0) {
filename =
filename.substring(filename.lastIndexOf('/') + 1);
}
// Identify the appBase of the owning Host of this Context
// (if any)
File file = new File(deployed, filename);
if (file.exists()) {
message = smClient.getString(
"htmlManagerServlet.deployUploadWarExists",
filename);
break;
}
ContextName cn = new ContextName(filename, true);
String name = cn.getName();
if ((host.findChild(name) != null) && !isDeployed(name)) {
message = smClient.getString(
"htmlManagerServlet.deployUploadInServerXml",
filename);
break;
}
if (isServiced(name)) {
message = smClient.getString("managerServlet.inService", name);
} else {
addServiced(name);
try {
warPart.write(file.getAbsolutePath());
// Perform new deployment
check(name);
} finally {
removeServiced(name);
}
}
break;
}
} catch(Exception e) {
message = smClient.getString
("htmlManagerServlet.deployUploadFail", e.getMessage());
log(message, e);
}
return message;
}
示例6: upload
import javax.servlet.http.Part; //导入方法依赖的package包/类
protected String upload(HttpServletRequest request, StringManager smClient) {
String message = "";
try {
while (true) {
Part warPart = request.getPart("deployWar");
if (warPart == null) {
message = smClient.getString("htmlManagerServlet.deployUploadNoFile");
break;
}
String filename = extractFilename(warPart.getHeader("Content-Disposition"));
if (!filename.toLowerCase(Locale.ENGLISH).endsWith(".war")) {
message = smClient.getString("htmlManagerServlet.deployUploadNotWar", filename);
break;
}
// Get the filename if uploaded name includes a path
if (filename.lastIndexOf('\\') >= 0) {
filename = filename.substring(filename.lastIndexOf('\\') + 1);
}
if (filename.lastIndexOf('/') >= 0) {
filename = filename.substring(filename.lastIndexOf('/') + 1);
}
// Identify the appBase of the owning Host of this Context
// (if any)
File file = new File(deployed, filename);
if (file.exists()) {
message = smClient.getString("htmlManagerServlet.deployUploadWarExists", filename);
break;
}
ContextName cn = new ContextName(filename, true);
String name = cn.getName();
if ((host.findChild(name) != null) && !isDeployed(name)) {
message = smClient.getString("htmlManagerServlet.deployUploadInServerXml", filename);
break;
}
if (isServiced(name)) {
message = smClient.getString("managerServlet.inService", name);
} else {
addServiced(name);
try {
warPart.write(file.getAbsolutePath());
// Perform new deployment
check(name);
} finally {
removeServiced(name);
}
}
break;
}
} catch (Exception e) {
message = smClient.getString("htmlManagerServlet.deployUploadFail", e.getMessage());
log(message, e);
}
return message;
}