当前位置: 代码迷 >> Java Web开发 >> tomcat服务器里的照片如何把它保存到硬盘里?
  详细解决方案

tomcat服务器里的照片如何把它保存到硬盘里?

热度:2086   发布时间:2016-04-10 23:40:52.0
tomcat服务器里的照片怎么把它保存到硬盘里??
求大神解释啊。急求急求!!!!!!
tomcat 服务器

------解决方案--------------------
引用:
Quote: 引用:

你都没说清楚图片存在哪,2进制是直接存在数据库中的,比如mysql mongodb等。这要写程序。
如果在myeclipse工作空间里,这意思是在硬盘上的。拷贝吧,少年



我说了我的目的就是把照片存到myeclipse工作空间里,。。拷贝不管用好吗。

自己学一下吧http://blog.sina.com.cn/s/blog_60afee6e010136dc.html
------解决方案--------------------
protected Map<String, Object> parseMultipart(HttpServletRequest request) {
Map<String, Object> map = new HashMap<String, Object>();
RequestContext ctx = new ServletRequestContext((HttpServletRequest) request);
if (!FileUpload.isMultipartContent(ctx))
return map;
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1024 * 1024 * 5);// 设置文件限制为5M
List<FileItem> items = upload.parseRequest((HttpServletRequest) request);
if (items != null) {
Map<String, List<Object>> tmp = new HashMap<String, List<Object>>();
for (FileItem item : items) {
Object value = item.isFormField() ? item.getString("UTF-8") : item;
if (!tmp.containsKey(item.getFieldName())) {
List<Object> values = new ArrayList<Object>();
tmp.put(item.getFieldName(), values);
}
tmp.get(item.getFieldName()).add(value);
}
if (tmp.size() > 0) {
for (String key : tmp.keySet()) {
List<Object> values = tmp.get(key);
if (values.size() == 1) {
map.put(key, values.get(0));
} else {
map.put(key, values.toArray());
}
}
}
}
} catch (Exception e) {
logger.error(e);
}
return map;
}

------解决方案--------------------
public void fileUpload2() {

String result = "OK";
// upload.setSizeMax(1024 * 1024 * 5);
HttpServletRequest request =  ServletActionContext
.getRequest();
String appName = request.getContextPath();
// 上传文件夹路径
String path = "../webapps" + appName + "/files/";
// 返回上传图片的网络地址
String basePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + appName + "/" + "files/";
String httpurl="";
try {


 
String fname =this.getFileStream2FileName();
//System.out.println(fname);
//System.out.println("$$$$$$$$$$$$$$$"+fileStream2.length);
String[] fnameList=fname.split(",");
for(int i=0;i<fileStream2.length;i++){
String path1 = path  + fnameList[i].trim();
String onrepath = basePath  + fnameList[i].trim();
httpurl=httpurl+onrepath+",";
File f = new File(path1);
if (!f.getParentFile().exists())
f.getParentFile().mkdirs();
f.createNewFile();
FileOutputStream fi = new FileOutputStream(f);
InputStream in = new FileInputStream(fileStream2[i]);;
byte buffer[] = new byte[8192];
int bytesRead = 0;
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
fi.write(buffer, 0, bytesRead);
}
in.close();
fi.close();
}

} catch (Exception e) {
e.printStackTrace();
result = "ERROR:Exception,请联系管理员!";

HttpServletResponse res = ServletActionContext.getResponse();
if ("OK".equals(result)) {
putJsondata(httpurl, res);
//System.out.println("333333333#################################"+httpurl);
} else {

putJsondata(result, res);
}


}
  相关解决方案