本文整理汇总了Java中org.apache.commons.fileupload.util.Streams.copy方法的典型用法代码示例。如果您正苦于以下问题:Java Streams.copy方法的具体用法?Java Streams.copy怎么用?Java Streams.copy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.fileupload.util.Streams
的用法示例。
在下文中一共展示了Streams.copy方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doPost
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletFileUpload upload = new ServletFileUpload();
try{
FileItemIterator iterator = upload.getItemIterator(request);
if (iterator.hasNext()) {
FileItemStream item = iterator.next();
String name = item.getFieldName();
logger.debug("Uploading file '{}' with item name '{}'", item.getName(), name);
InputStream stream = item.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.copy(stream, out, true);
byte[] data = out.toByteArray();
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
response.getWriter().print(new String(data));
response.flushBuffer();
}
else {
logger.error("No file found in post request!");
throw new RuntimeException("No file found in post request!");
}
}
catch(Exception e){
logger.error("Unexpected error in FileUploadServlet.doPost: ", e);
throw new RuntimeException(e);
}
}
示例2: doPost
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//CHECKSTYLE:ON
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator iter = upload.getItemIterator(request);
if (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
LOG.debug("Uploading file '{}' with item name '{}'", item.getName(), name);
InputStream stream = item.openStream();
// Process the input stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.copy(stream, out, true);
byte[] data = out.toByteArray();
cacheService.uploadedFile(name, data);
} else {
LOG.error("No file found in post request!");
throw new RuntimeException("No file found in post request!");
}
} catch (Exception ex) {
LOG.error("Unexpected error in FileUpload.doPost: ", ex);
throw new RuntimeException(ex);
}
}
示例3: doGet
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
/**
* 实现多文件的同时上传
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("===========");
//设置接收的编码格式
request.setCharacterEncoding("UTF-8");
Date date = new Date();//获取当前时间
SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat sdfFolder = new SimpleDateFormat("yyMM");
// String newfileName = sdfFileName.format(date);//文件名称
String fileRealPath = "";//文件存放真实地址
String fileRealResistPath = "";//文件存放真实相对路径
//名称 界面编码 必须 和request 保存一致..否则乱码
String name = request.getParameter("name");
String id = request.getParameter("id"); //内容的ID,必须先添加内容,然后才能上传图片
// String newfileName = name;
String firstFileName="";
// 获得容器中上传文件夹所在的物理路径
String savePath = this.getServletConfig().getServletContext().getRealPath("/") + "upload\\" + id +"\\";
System.out.println("路径" + savePath+"; name:"+name);
File file = new File(savePath);
if (!file.isDirectory()) {
file.mkdirs();
}
try {
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("UTF-8");
// upload.setFileItemFactory(factory)
System.out.println("request:="+request);
// 获取多个上传文件
List fileList = fileList = upload.parseRequest(request);
System.out.println("fileList:"+fileList);
// 遍历上传文件写入磁盘
Iterator it = fileList.iterator();
while (it.hasNext()) {
Object obit = it.next();
if(obit instanceof DiskFileItem){
System.out.println("xxxxxxxxxxxxx");
DiskFileItem item = (DiskFileItem) obit;
// 如果item是文件上传表单域
// 获得文件名及路径
String fileName = item.getName();
if (fileName != null) {
firstFileName=item.getName().substring(item.getName().lastIndexOf("\\")+1);
String formatName = firstFileName.substring(firstFileName.lastIndexOf("."));//获取文件后缀名
fileRealPath = savePath + fileName;//+ formatName;//文件存放真实地址
BufferedInputStream in = new BufferedInputStream(item.getInputStream());// 获得文件输入流
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(fileRealPath)));// 获得文件输出流
Streams.copy(in, outStream, true);// 开始把文件写到你指定的上传文件夹
//上传成功,则插入数据库
if (new File(fileRealPath).exists()) {
//虚拟路径赋值
fileRealResistPath=sdfFolder.format(date)+"/"+fileRealPath.substring(fileRealPath.lastIndexOf("\\")+1);
//保存到数据库
System.out.println("保存到数据库:");
System.out.println("name:"+name);
System.out.println("虚拟路径:"+fileRealResistPath);
}
}
}
}
} catch (org.apache.commons.fileupload.FileUploadException ex) {
ex.printStackTrace();
System.out.println("没有上传文件");
return;
}
response.getWriter().write("1");
}
示例4: readBodyData
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
/**
* <p>Reads <code>body-data</code> from the current
* <code>encapsulation</code> and writes its contents into the
* output <code>Stream</code>.
* <p>
* <p>Arbitrary large amounts of data can be processed by this
* method using a constant size buffer. (see {@link
* #MultipartStream(InputStream, byte[], int,
* MultipartStreamCopy.ProgressNotifier) constructor}).
*
* @param output The <code>Stream</code> to write data into. May
* be null, in which case this method is equivalent
* to {@link #discardBodyData()}.
* @return the amount of data written.
* @throws MalformedStreamException if the stream ends unexpectedly.
* @throws IOException if an i/o error occurs.
*/
public int readBodyData(OutputStream output)
throws MalformedStreamException, IOException {
final InputStream istream = newInputStream();
return (int) Streams.copy(istream, output, false);
}
示例5: readBodyData
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
/**
* <p>Reads <code>body-data</code> from the current
* <code>encapsulation</code> and writes its contents into the
* output <code>Stream</code>.
*
* <p>Arbitrary large amounts of data can be processed by this
* method using a constant size buffer. (see {@link
* #MultipartStream(InputStream,byte[],int,
* MultipartStream.ProgressNotifier) constructor}).
*
* @param output The <code>Stream</code> to write data into. May
* be null, in which case this method is equivalent
* to {@link #discardBodyData()}.
*
* @return the amount of data written.
*
* @throws MalformedStreamException if the stream ends unexpectedly.
* @throws IOException if an i/o error occurs.
*/
public int readBodyData(OutputStream output)
throws MalformedStreamException, IOException {
final InputStream istream = newInputStream();
return (int) Streams.copy(istream, output, false);
}
示例6: readBodyData
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
/**
* <p>Reads <code>body-data</code> from the current
* <code>encapsulation</code> and writes its contents into the
* output <code>Stream</code>.
*
* <p>Arbitrary large amounts of data can be processed by this
* method using a constant size buffer. (see {@link
* #MultipartStream(InputStream,byte[],int, ProgressNotifier) constructor}).
*
* @param output The <code>Stream</code> to write data into. May
* be null, in which case this method is equivalent
* to {@link #discardBodyData()}.
*
* @return the amount of data written.
*
* @throws MalformedStreamException if the stream ends unexpectedly.
* @throws IOException if an i/o error occurs.
*/
public int readBodyData(OutputStream output)
throws MalformedStreamException, IOException {
final InputStream istream = newInputStream();
return (int) Streams.copy(istream, output, false);
}